rtlfield.cpp 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include <math.h>
  15. #include <stdio.h>
  16. #include <atomic>
  17. #include <algorithm>
  18. #include "jmisc.hpp"
  19. #include "jlib.hpp"
  20. #include "eclhelper.hpp"
  21. #include "eclrtl_imp.hpp"
  22. #include "rtlfield.hpp"
  23. #include "rtlds_imp.hpp"
  24. #include "rtlrecord.hpp"
  25. #include "rtlkey.hpp"
  26. #include "nbcd.hpp"
  27. #include "rtlqstr.ipp"
  28. static const char * queryXPath(const RtlFieldInfo * field)
  29. {
  30. const char * xpath = field->xpath;
  31. if (xpath)
  32. {
  33. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  34. if (!sep)
  35. return xpath;
  36. return sep+1;
  37. }
  38. return field->name;
  39. }
  40. static const char * queryScalarXPath(const RtlFieldInfo * field)
  41. {
  42. if (field->hasNonScalarXpath())
  43. return field->name;
  44. return queryXPath(field);
  45. }
  46. static bool hasOuterXPath(const RtlFieldInfo * field)
  47. {
  48. const char * xpath = field->xpath;
  49. assertex(xpath);
  50. return (*xpath != xpathCompoundSeparatorChar);
  51. }
  52. static void queryNestedOuterXPath(StringAttr & ret, const RtlFieldInfo * field)
  53. {
  54. const char * xpath = field->xpath;
  55. assertex(xpath);
  56. const char * sep = strchr(xpath, xpathCompoundSeparatorChar);
  57. assertex(sep);
  58. ret.set(xpath, (size32_t)(sep-xpath));
  59. }
  60. inline const char * queryName(const RtlFieldInfo * field) { return field ? field->name : nullptr; }
  61. //-------------------------------------------------------------------------------------------------------------------
  62. static bool incrementBuffer(byte *buf, size32_t size)
  63. {
  64. int i = size;
  65. while (i--)
  66. {
  67. buf[i]++;
  68. if (buf[i]!=0)
  69. return true;
  70. }
  71. return false;
  72. }
  73. static bool decrementBuffer(byte *buf, size32_t size)
  74. {
  75. int i = size;
  76. while (i--)
  77. {
  78. buf[i]--;
  79. if (buf[i]!=0xff)
  80. return true;
  81. }
  82. return false;
  83. }
  84. //-------------------------------------------------------------------------------------------------------------------
  85. class DummyFieldProcessor : public CInterfaceOf<IFieldProcessor>
  86. {
  87. public:
  88. virtual void processString(unsigned len, const char *value, const RtlFieldInfo * field) {}
  89. virtual void processBool(bool value, const RtlFieldInfo * field) {}
  90. virtual void processData(unsigned len, const void *value, const RtlFieldInfo * field) {}
  91. virtual void processInt(__int64 value, const RtlFieldInfo * field) {}
  92. virtual void processUInt(unsigned __int64 value, const RtlFieldInfo * field) {}
  93. virtual void processReal(double value, const RtlFieldInfo * field) {}
  94. virtual void processDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field) {}
  95. virtual void processUDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field) {}
  96. virtual void processUnicode(unsigned len, const UChar *value, const RtlFieldInfo * field) {}
  97. virtual void processQString(unsigned len, const char *value, const RtlFieldInfo * field) {}
  98. virtual void processUtf8(unsigned len, const char *value, const RtlFieldInfo * field) {}
  99. virtual bool processBeginSet(const RtlFieldInfo * field, unsigned numElements, bool isAll, const byte *data) { return false; }
  100. virtual bool processBeginDataset(const RtlFieldInfo * field, unsigned numRows) { return true; }
  101. virtual bool processBeginRow(const RtlFieldInfo * field) { return true; }
  102. virtual void processEndSet(const RtlFieldInfo * field) {}
  103. virtual void processEndDataset(const RtlFieldInfo * field) {}
  104. virtual void processEndRow(const RtlFieldInfo * field) {}
  105. };
  106. //-------------------------------------------------------------------------------------------------------------------
  107. size32_t ECLRTL_API getMinSize(const RtlFieldInfo * const * fields)
  108. {
  109. size32_t minSize = 0;
  110. for(;;)
  111. {
  112. const RtlFieldInfo * cur = *fields;
  113. if (!cur)
  114. return minSize;
  115. minSize += cur->type->getMinSize();
  116. fields++;
  117. }
  118. }
  119. //-------------------------------------------------------------------------------------------------------------------
  120. size32_t RtlTypeInfoBase::getMinSize() const
  121. {
  122. return length;
  123. }
  124. size32_t RtlTypeInfoBase::size(const byte * self, const byte * selfrow) const
  125. {
  126. return length;
  127. }
  128. size32_t RtlTypeInfoBase::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  129. {
  130. rtlFailUnexpected();
  131. return 0;
  132. }
  133. size32_t RtlTypeInfoBase::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & out) const
  134. {
  135. rtlFailUnexpected();
  136. return 0;
  137. }
  138. size32_t RtlTypeInfoBase::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  139. {
  140. rtlFailUnexpected();
  141. return 0;
  142. }
  143. size32_t RtlTypeInfoBase::buildNull(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field) const
  144. {
  145. if (field->initializer && !isVirtualInitializer(field->initializer))
  146. {
  147. size32_t initSize = size(field->initializer, nullptr);
  148. builder.ensureCapacity(offset+initSize, queryName(field));
  149. memcpy(builder.getSelf()+offset, field->initializer, initSize);
  150. return offset+initSize;
  151. }
  152. else
  153. {
  154. // This code covers a lot (though not all) of the derived cases
  155. size32_t initSize = getMinSize();
  156. builder.ensureCapacity(offset+initSize, queryName(field));
  157. memset(builder.getSelf() + offset, 0, initSize);
  158. return offset + initSize;
  159. }
  160. }
  161. size32_t RtlTypeInfoBase::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  162. {
  163. size32_t newLen;
  164. rtlDataAttr temp;
  165. rtlInt8ToStrX(newLen, temp.refstr(), val);
  166. return buildString(builder, offset, field, newLen, temp.getstr());
  167. }
  168. size32_t RtlTypeInfoBase::buildReal(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, double val) const
  169. {
  170. size32_t newLen;
  171. rtlDataAttr temp;
  172. rtlRealToStrX(newLen, temp.refstr(), val);
  173. return buildString(builder, offset, field, newLen, temp.getstr());
  174. }
  175. double RtlTypeInfoBase::getReal(const void * ptr) const
  176. {
  177. size32_t len;
  178. rtlDataAttr value;
  179. getString(len, value.refstr(), ptr);
  180. return rtlStrToReal(len, value.getstr());
  181. }
  182. bool RtlTypeInfoBase::equivalent(const RtlTypeInfo *to) const
  183. {
  184. if (to==this)
  185. return true;
  186. if (!to)
  187. return false;
  188. if (to->length != length || to->fieldType != fieldType)
  189. return false;
  190. if (queryLocale())
  191. {
  192. // do we permit a locale difference?
  193. }
  194. auto child = queryChildType();
  195. if (child && !child->equivalent(to->queryChildType()))
  196. return false;
  197. auto fields = queryFields();
  198. if (fields)
  199. {
  200. auto tofields = to->queryFields();
  201. if (!tofields)
  202. return false; // Should never happen
  203. for (unsigned idx = 0; fields[idx]; idx++)
  204. {
  205. if (!fields[idx]->equivalent(tofields[idx]))
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. static bool strequivalent(const char *name1, const char *name2)
  212. {
  213. if (name1)
  214. return name2 && streq(name1, name2);
  215. else
  216. return name2==nullptr;
  217. }
  218. bool RtlFieldInfo::equivalent(const RtlFieldInfo *to) const
  219. {
  220. if (to==this)
  221. return true;
  222. if (!to)
  223. return false;
  224. if (!strequivalent(name, to->name))
  225. return false;
  226. if (!strequivalent(xpath, to->xpath))
  227. return false;
  228. if (!type->equivalent(to->type))
  229. return false;
  230. // Initializer differences can be ignored
  231. if (flags != to->flags)
  232. return false;
  233. return true;
  234. }
  235. const char * RtlTypeInfoBase::queryLocale() const
  236. {
  237. return NULL;
  238. }
  239. bool RtlTypeInfoBase::isScalar() const
  240. {
  241. return true;
  242. }
  243. const RtlFieldInfo * const * RtlTypeInfoBase::queryFields() const
  244. {
  245. return NULL;
  246. }
  247. const RtlTypeInfo * RtlTypeInfoBase::queryChildType() const
  248. {
  249. return NULL;
  250. }
  251. const IFieldFilter * RtlTypeInfoBase::queryFilter() const
  252. {
  253. return nullptr;
  254. }
  255. size32_t RtlTypeInfoBase::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  256. {
  257. size32_t thisSize = size(nullptr, nullptr);
  258. byte * dest = builder.ensureCapacity(offset + thisSize, nullptr) + offset;
  259. in.read(thisSize, dest);
  260. return offset + thisSize;
  261. }
  262. void RtlTypeInfoBase::readAhead(IRowPrefetcherSource & in) const
  263. {
  264. size32_t thisSize = size(nullptr, nullptr);
  265. in.skip(thisSize);
  266. }
  267. size32_t RtlTypeInfoBase::buildUtf8ViaString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  268. {
  269. size32_t newLen;
  270. rtlDataAttr temp;
  271. rtlUtf8ToStrX(newLen, temp.refstr(), len, value);
  272. return buildString(builder, offset, field, newLen, temp.getstr());
  273. }
  274. void RtlTypeInfoBase::getUtf8ViaString(size32_t & resultLen, char * & result, const void * ptr) const
  275. {
  276. size32_t newLen;
  277. rtlDataAttr temp;
  278. getString(newLen, temp.refstr(), ptr);
  279. rtlStrToUtf8X(resultLen, result, newLen, temp.getstr());
  280. }
  281. int RtlTypeInfoBase::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  282. {
  283. throwUnexpected();
  284. }
  285. void RtlTypeInfoBase::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  286. {
  287. byte *dst = (byte *) buffer;
  288. size32_t minSize = getMinSize();
  289. if (value)
  290. {
  291. assertex(isFixedSize() || (inclusive && (subLength == MatchFullString)));
  292. size32_t copySize;
  293. if (subLength != MatchFullString)
  294. {
  295. if (isFixedSize())
  296. {
  297. //For a substring comparison, clear the bytes after subLength - eventually they should never be compared
  298. //but currently the index code uses memcmp() so they need to be initialised
  299. unsigned copyLen = std::min(length, subLength);
  300. copySize = copyLen;
  301. memcpy(dst, value, copySize);
  302. memset(dst+copySize, fill, minSize-copySize);
  303. }
  304. else
  305. {
  306. assertex(inclusive);
  307. size32_t copyLen = std::min(rtlReadSize32t(value), subLength);
  308. copySize = copyLen;
  309. *(size32_t *)buffer = copyLen;
  310. memcpy(dst + sizeof(size32_t), value + sizeof(size32_t), copySize);
  311. }
  312. }
  313. else
  314. {
  315. copySize = size(value, nullptr);
  316. memcpy(dst, value, copySize);
  317. }
  318. if (!inclusive)
  319. {
  320. if (fill == 0)
  321. incrementBuffer(dst, copySize);
  322. else
  323. decrementBuffer(dst, copySize);
  324. }
  325. }
  326. else
  327. {
  328. //Generally cannot work for variable length fields - since always possible to create a larger value
  329. //Default does not work for real types, or variable length signed integers
  330. assertex((fill == 0) || isFixedSize());
  331. memset(dst, fill, minSize);
  332. }
  333. }
  334. void RtlTypeInfoBase::setLowBound(void * buffer, const byte * value, size32_t subLength, bool inclusive) const
  335. {
  336. setBound(buffer, value, subLength, 0, inclusive);
  337. }
  338. void RtlTypeInfoBase::setHighBound(void * buffer, const byte * value, size32_t subLength, bool inclusive) const
  339. {
  340. setBound(buffer, value, subLength, 0xff, inclusive);
  341. }
  342. //-------------------------------------------------------------------------------------------------------------------
  343. size32_t RtlBoolTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  344. {
  345. builder.ensureCapacity(sizeof(bool)+offset, queryName(field));
  346. bool val = source.getBooleanResult(field);
  347. * (bool *) (builder.getSelf() + offset) = val;
  348. offset += sizeof(bool);
  349. return offset;
  350. }
  351. size32_t RtlBoolTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  352. {
  353. builder.ensureCapacity(sizeof(bool)+offset, queryName(field));
  354. * (bool *) (builder.getSelf() + offset) = val != 0;
  355. offset += sizeof(bool);
  356. return offset;
  357. }
  358. size32_t RtlBoolTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  359. {
  360. return buildInt(builder, offset, field, rtlStrToBool(len, value));
  361. }
  362. size32_t RtlBoolTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  363. {
  364. size32_t size = rtlUtf8Length(len, value);
  365. return buildInt(builder, offset, field, rtlStrToBool(size, value));
  366. }
  367. size32_t RtlBoolTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  368. {
  369. target.processBool(*(const bool *)self, field);
  370. return sizeof(bool);
  371. }
  372. size32_t RtlBoolTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  373. {
  374. target.outputBool(*(const bool *)self, queryScalarXPath(field));
  375. return sizeof(bool);
  376. }
  377. void RtlBoolTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  378. {
  379. const bool * cast = static_cast<const bool *>(ptr);
  380. rtlBoolToStrX(resultLen, result, *cast);
  381. }
  382. void RtlBoolTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  383. {
  384. getString(resultLen, result, ptr);
  385. }
  386. __int64 RtlBoolTypeInfo::getInt(const void * ptr) const
  387. {
  388. const bool * cast = static_cast<const bool *>(ptr);
  389. return (__int64)*cast;
  390. }
  391. bool RtlBoolTypeInfo::getBool(const void * ptr) const
  392. {
  393. const bool * cast = static_cast<const bool *>(ptr);
  394. return *cast;
  395. }
  396. int RtlBoolTypeInfo::compare(const byte * left, const byte * right) const
  397. {
  398. bool leftValue = getBool(left);
  399. bool rightValue = getBool (right);
  400. return (!leftValue && rightValue) ? -1 : (leftValue && !rightValue) ? +1 : 0;
  401. }
  402. unsigned RtlBoolTypeInfo::hash(const byte *self, unsigned inhash) const
  403. {
  404. __int64 val = getInt(self);
  405. return rtlHash32Data8(&val, inhash);
  406. }
  407. //-------------------------------------------------------------------------------------------------------------------
  408. double RtlRealTypeInfo::value(const void * self) const
  409. {
  410. if (length == 4)
  411. return *(const float *)self;
  412. return *(const double *)self;
  413. }
  414. size32_t RtlRealTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  415. {
  416. double val = source.getRealResult(field);
  417. return buildReal(builder, offset, field, val);
  418. }
  419. size32_t RtlRealTypeInfo::buildReal(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, double val) const
  420. {
  421. byte *dest = builder.ensureCapacity(length+offset, queryName(field)) + offset;
  422. if (length == 4)
  423. *(float *) dest = (float) val;
  424. else
  425. *(double *) dest = val;
  426. offset += length;
  427. return offset;
  428. }
  429. size32_t RtlRealTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  430. {
  431. return buildReal(builder, offset, field, rtlStrToReal(len, value));
  432. }
  433. size32_t RtlRealTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  434. {
  435. size32_t size = rtlUtf8Length(len, value);
  436. return buildReal(builder, offset, field, rtlStrToReal(size, value));
  437. }
  438. size32_t RtlRealTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  439. {
  440. target.processReal(value(self), field);
  441. return length;
  442. }
  443. size32_t RtlRealTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  444. {
  445. target.outputReal(value(self), queryScalarXPath(field));
  446. return length;
  447. }
  448. void RtlRealTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  449. {
  450. double num = value(ptr);
  451. rtlRealToStrX(resultLen, result, num);
  452. }
  453. void RtlRealTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  454. {
  455. getString(resultLen, result, ptr);
  456. }
  457. __int64 RtlRealTypeInfo::getInt(const void * ptr) const
  458. {
  459. double num = value(ptr);
  460. return (__int64)num;
  461. }
  462. double RtlRealTypeInfo::getReal(const void * ptr) const
  463. {
  464. return value(ptr);
  465. }
  466. int RtlRealTypeInfo::compare(const byte * left, const byte * right) const
  467. {
  468. double leftValue = getReal(left);
  469. double rightValue = getReal(right);
  470. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  471. }
  472. unsigned RtlRealTypeInfo::hash(const byte *self, unsigned inhash) const
  473. {
  474. double val = getReal(self);
  475. return rtlHash32Data8(&val, inhash);
  476. }
  477. void RtlRealTypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  478. {
  479. assertex(subLength == MatchFullString);
  480. double dvalue;
  481. float fvalue;
  482. if (value)
  483. {
  484. assertex(inclusive);
  485. }
  486. else
  487. {
  488. dvalue = rtlCreateRealInf();
  489. if (fill == 0)
  490. dvalue = -dvalue;
  491. if (length == 4)
  492. {
  493. fvalue = dvalue;
  494. value = reinterpret_cast<const byte *>(&fvalue);
  495. }
  496. else
  497. value = reinterpret_cast<const byte *>(&dvalue);
  498. }
  499. memcpy(buffer, value, length);
  500. }
  501. //-------------------------------------------------------------------------------------------------------------------
  502. size32_t RtlIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  503. {
  504. builder.ensureCapacity(length+offset, queryName(field));
  505. __int64 val = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  506. rtlWriteInt(builder.getSelf() + offset, val, length);
  507. offset += length;
  508. return offset;
  509. }
  510. size32_t RtlIntTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  511. {
  512. builder.ensureCapacity(length+offset, queryName(field));
  513. rtlWriteInt(builder.getSelf() + offset, val, length);
  514. offset += length;
  515. return offset;
  516. }
  517. size32_t RtlIntTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  518. {
  519. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  520. }
  521. size32_t RtlIntTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  522. {
  523. size32_t size = rtlUtf8Length(len, value);
  524. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  525. }
  526. size32_t RtlIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  527. {
  528. if (isUnsigned())
  529. target.processUInt(rtlReadUInt(self, length), field);
  530. else
  531. target.processInt(rtlReadInt(self, length), field);
  532. return length;
  533. }
  534. size32_t RtlIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  535. {
  536. if (isUnsigned())
  537. target.outputUInt(rtlReadUInt(self, length), length, queryScalarXPath(field));
  538. else
  539. target.outputInt(rtlReadInt(self, length), length, queryScalarXPath(field));
  540. return length;
  541. }
  542. void RtlIntTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  543. {
  544. if (isUnsigned())
  545. rtlUInt8ToStrX(resultLen, result, rtlReadUInt(ptr, length));
  546. else
  547. rtlInt8ToStrX(resultLen, result, rtlReadInt(ptr, length));
  548. }
  549. void RtlIntTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  550. {
  551. getString(resultLen, result, ptr);
  552. }
  553. __int64 RtlIntTypeInfo::getInt(const void * ptr) const
  554. {
  555. if (isUnsigned())
  556. return rtlReadUInt(ptr, length);
  557. else
  558. return rtlReadInt(ptr, length);
  559. }
  560. double RtlIntTypeInfo::getReal(const void * ptr) const
  561. {
  562. if (isUnsigned())
  563. return (double) rtlReadUInt(ptr, length);
  564. else
  565. return (double) rtlReadInt(ptr, length);
  566. }
  567. int RtlIntTypeInfo::compare(const byte * left, const byte * right) const
  568. {
  569. if (isUnsigned())
  570. {
  571. unsigned __int64 leftValue = rtlReadUInt(left, length);
  572. unsigned __int64 rightValue = rtlReadUInt(right, length);
  573. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  574. }
  575. else
  576. {
  577. __int64 leftValue = rtlReadInt(left, length);
  578. __int64 rightValue = rtlReadInt(right, length);
  579. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  580. }
  581. }
  582. bool RtlIntTypeInfo::canMemCmp() const
  583. {
  584. #if __BYTE_ORDER == __LITTLE_ENDIAN
  585. return false;
  586. #else
  587. return isUnsigned();
  588. #endif
  589. }
  590. unsigned RtlIntTypeInfo::hash(const byte *self, unsigned inhash) const
  591. {
  592. __int64 val = getInt(self);
  593. return rtlHash32Data8(&val, inhash);
  594. }
  595. bool RtlIntTypeInfo::canTruncate() const
  596. {
  597. #if __BYTE_ORDER == __LITTLE_ENDIAN
  598. return true;
  599. #else
  600. return false;
  601. #endif
  602. }
  603. bool RtlIntTypeInfo::canExtend(char &fillChar) const
  604. {
  605. #if __BYTE_ORDER == __LITTLE_ENDIAN
  606. fillChar = 0;
  607. return true;
  608. #else
  609. return false;
  610. #endif
  611. }
  612. //-------------------------------------------------------------------------------------------------------------------
  613. size32_t RtlBlobTypeInfo::getMinSize() const
  614. {
  615. return sizeof(offset_t);
  616. }
  617. size32_t RtlBlobTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  618. {
  619. throwUnexpected(); // This is only expected to be used for reading at present
  620. }
  621. size32_t RtlBlobTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  622. {
  623. throwUnexpected(); // This is only expected to be used for reading at present
  624. }
  625. size32_t RtlBlobTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  626. {
  627. throwUnexpected(); // This is only expected to be used for reading at present
  628. }
  629. size32_t RtlBlobTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  630. {
  631. throwUnexpected(); // This is only expected to be used for reading at present
  632. }
  633. size32_t RtlBlobTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  634. {
  635. UNIMPLEMENTED;
  636. }
  637. size32_t RtlBlobTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  638. {
  639. UNIMPLEMENTED;
  640. }
  641. void RtlBlobTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  642. {
  643. UNIMPLEMENTED;
  644. }
  645. void RtlBlobTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  646. {
  647. UNIMPLEMENTED;
  648. }
  649. __int64 RtlBlobTypeInfo::getInt(const void * ptr) const
  650. {
  651. return *(offset_t *) ptr;
  652. }
  653. double RtlBlobTypeInfo::getReal(const void * ptr) const
  654. {
  655. UNIMPLEMENTED;
  656. }
  657. bool RtlBlobTypeInfo::canTruncate() const
  658. {
  659. return false;
  660. }
  661. bool RtlBlobTypeInfo::canExtend(char &fillChar) const
  662. {
  663. return false;
  664. }
  665. int RtlBlobTypeInfo::compare(const byte * left, const byte * right) const
  666. {
  667. UNIMPLEMENTED;
  668. }
  669. unsigned RtlBlobTypeInfo::hash(const byte *self, unsigned inhash) const
  670. {
  671. UNIMPLEMENTED;
  672. }
  673. //-------------------------------------------------------------------------------------------------------------------
  674. size32_t RtlSwapIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  675. {
  676. builder.ensureCapacity(length+offset, queryName(field));
  677. __int64 val = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  678. // NOTE - we assume that the value returned from the source is NOT already a swapped int - source doesn't know that we are going to store it swapped
  679. rtlWriteSwapInt(builder.getSelf() + offset, val, length);
  680. offset += length;
  681. return offset;
  682. }
  683. size32_t RtlSwapIntTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  684. {
  685. builder.ensureCapacity(length+offset, queryName(field));
  686. rtlWriteSwapInt(builder.getSelf() + offset, val, length);
  687. offset += length;
  688. return offset;
  689. }
  690. size32_t RtlSwapIntTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  691. {
  692. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  693. }
  694. size32_t RtlSwapIntTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  695. {
  696. size32_t size = rtlUtf8Length(len, value);
  697. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  698. }
  699. size32_t RtlSwapIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  700. {
  701. if (isUnsigned())
  702. target.processUInt(rtlReadSwapUInt(self, length), field);
  703. else
  704. target.processInt(rtlReadSwapInt(self, length), field);
  705. return length;
  706. }
  707. size32_t RtlSwapIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  708. {
  709. if (isUnsigned())
  710. target.outputUInt(rtlReadSwapUInt(self, length), length, queryScalarXPath(field));
  711. else
  712. target.outputInt(rtlReadSwapInt(self, length), length, queryScalarXPath(field));
  713. return length;
  714. }
  715. void RtlSwapIntTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  716. {
  717. if (isUnsigned())
  718. rtlUInt8ToStrX(resultLen, result, rtlReadSwapUInt(ptr, length));
  719. else
  720. rtlInt8ToStrX(resultLen, result, rtlReadSwapInt(ptr, length));
  721. }
  722. void RtlSwapIntTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  723. {
  724. getString(resultLen, result, ptr);
  725. }
  726. __int64 RtlSwapIntTypeInfo::getInt(const void * ptr) const
  727. {
  728. if (isUnsigned())
  729. return rtlReadSwapUInt(ptr, length);
  730. else
  731. return rtlReadSwapInt(ptr, length);
  732. }
  733. double RtlSwapIntTypeInfo::getReal(const void * ptr) const
  734. {
  735. if (isUnsigned())
  736. return (double) rtlReadSwapUInt(ptr, length);
  737. else
  738. return (double) (rtlReadSwapInt(ptr, length));
  739. }
  740. int RtlSwapIntTypeInfo::compare(const byte * left, const byte * right) const
  741. {
  742. if (isUnsigned())
  743. {
  744. unsigned __int64 leftValue = rtlReadSwapUInt(left, length);
  745. unsigned __int64 rightValue = rtlReadSwapUInt(right, length);
  746. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  747. }
  748. else
  749. {
  750. __int64 leftValue = rtlReadSwapInt(left, length);
  751. __int64 rightValue = rtlReadSwapInt(right, length);
  752. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  753. }
  754. }
  755. bool RtlSwapIntTypeInfo::canMemCmp() const
  756. {
  757. #if __BYTE_ORDER == __LITTLE_ENDIAN
  758. return isUnsigned();
  759. #else
  760. return false;
  761. #endif
  762. }
  763. unsigned RtlSwapIntTypeInfo::hash(const byte *self, unsigned inhash) const
  764. {
  765. __int64 val = getInt(self);
  766. return rtlHash32Data8(&val, inhash);
  767. }
  768. bool RtlSwapIntTypeInfo::canTruncate() const
  769. {
  770. #if __BYTE_ORDER == __LITTLE_ENDIAN
  771. return false;
  772. #else
  773. return true;
  774. #endif
  775. }
  776. bool RtlSwapIntTypeInfo::canExtend(char &fillChar) const
  777. {
  778. #if __BYTE_ORDER == __LITTLE_ENDIAN
  779. return false;
  780. #else
  781. fillChar = 0;
  782. return true;
  783. #endif
  784. }
  785. //-------------------------------------------------------------------------------------------------------------------
  786. size32_t RtlKeyedIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  787. {
  788. __int64 val = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  789. return buildInt(builder, offset, field, val);
  790. }
  791. size32_t RtlKeyedIntTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  792. {
  793. builder.ensureCapacity(length+offset, queryName(field));
  794. if (isUnsigned())
  795. #if __BYTE_ORDER == __LITTLE_ENDIAN
  796. rtlWriteSwapInt(builder.getSelf() + offset, val, length);
  797. #else
  798. rtlWriteInt(builder.getSelf() + offset, val, length);
  799. #endif
  800. else
  801. #if __BYTE_ORDER == __LITTLE_ENDIAN
  802. rtlWriteSwapInt(builder.getSelf() + offset, addBias(val, length), length);
  803. #else
  804. rtlWriteInt(builder.getSelf() + offset, addBias(val, length), length);
  805. #endif
  806. offset += length;
  807. return offset;
  808. }
  809. size32_t RtlKeyedIntTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  810. {
  811. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  812. }
  813. size32_t RtlKeyedIntTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  814. {
  815. size32_t size = rtlUtf8Length(len, value);
  816. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  817. }
  818. size32_t RtlKeyedIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  819. {
  820. if (isUnsigned())
  821. target.processUInt(getUInt(self), field);
  822. else
  823. target.processInt(getInt(self), field);
  824. return length;
  825. }
  826. size32_t RtlKeyedIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  827. {
  828. if (isUnsigned())
  829. target.outputUInt(getUInt(self), length, queryScalarXPath(field));
  830. else
  831. target.outputInt(getInt(self), length, queryScalarXPath(field));
  832. return length;
  833. }
  834. void RtlKeyedIntTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  835. {
  836. if (isUnsigned())
  837. rtlUInt8ToStrX(resultLen, result, getUInt(ptr));
  838. else
  839. rtlInt8ToStrX(resultLen, result, getInt(ptr));
  840. }
  841. void RtlKeyedIntTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  842. {
  843. getString(resultLen, result, ptr);
  844. }
  845. __int64 RtlKeyedIntTypeInfo::getInt(const void * ptr) const
  846. {
  847. #if __BYTE_ORDER == __LITTLE_ENDIAN
  848. if (isUnsigned())
  849. return rtlReadSwapUInt(ptr, length);
  850. else
  851. return removeBias(rtlReadSwapUInt(ptr, length), length);
  852. #else
  853. if (isUnsigned())
  854. return rtlReadUInt(ptr, length);
  855. else
  856. return removeBias(rtlReadInt(ptr, length), length);
  857. #endif
  858. }
  859. double RtlKeyedIntTypeInfo::getReal(const void * ptr) const
  860. {
  861. if (isUnsigned())
  862. return (double) getUInt(ptr);
  863. else
  864. return (double) getInt(ptr);
  865. }
  866. int RtlKeyedIntTypeInfo::compare(const byte * left, const byte * right) const
  867. {
  868. // The whole point of biased ints is that we can do this:
  869. return memcmp(left, right, length);
  870. }
  871. unsigned RtlKeyedIntTypeInfo::hash(const byte *self, unsigned inhash) const
  872. {
  873. __int64 val = getInt(self);
  874. return rtlHash32Data8(&val, inhash);
  875. }
  876. unsigned __int64 RtlKeyedIntTypeInfo::addBias(__int64 value, unsigned length)
  877. {
  878. return value + ((unsigned __int64)1 << (length*8-1));
  879. }
  880. __int64 RtlKeyedIntTypeInfo::removeBias(unsigned __int64 value, unsigned length)
  881. {
  882. return value - ((unsigned __int64)1 << (length*8-1));
  883. }
  884. //-------------------------------------------------------------------------------------------------------------------
  885. size32_t RtlPackedIntTypeInfo::getMinSize() const
  886. {
  887. return 1;
  888. }
  889. size32_t RtlPackedIntTypeInfo::size(const byte * self, const byte * selfrow) const
  890. {
  891. return rtlGetPackedSize(self);
  892. }
  893. size32_t RtlPackedIntTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  894. {
  895. __int64 value = isUnsigned() ? (__int64) source.getUnsignedResult(field) : source.getSignedResult(field);
  896. return buildInt(builder, offset, field, value);
  897. }
  898. size32_t RtlPackedIntTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  899. {
  900. size32_t sizeInBytes = rtlGetPackedSize(&val);
  901. builder.ensureCapacity(sizeInBytes+offset, queryName(field));
  902. rtlSetPackedUnsigned(builder.getSelf() + offset, val);
  903. offset += sizeInBytes;
  904. return offset;
  905. }
  906. size32_t RtlPackedIntTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  907. {
  908. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  909. }
  910. size32_t RtlPackedIntTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  911. {
  912. size32_t size = rtlUtf8Length(len, value);
  913. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  914. }
  915. size32_t RtlPackedIntTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  916. {
  917. if (isUnsigned())
  918. target.processUInt(rtlGetPackedUnsigned(self), field);
  919. else
  920. target.processInt(rtlGetPackedSigned(self), field);
  921. return rtlGetPackedSize(self);
  922. }
  923. size32_t RtlPackedIntTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  924. {
  925. size32_t fieldsize = rtlGetPackedSize(self);
  926. if (isUnsigned())
  927. target.outputUInt(rtlGetPackedUnsigned(self), fieldsize, queryScalarXPath(field));
  928. else
  929. target.outputInt(rtlGetPackedSigned(self), fieldsize, queryScalarXPath(field));
  930. return fieldsize;
  931. }
  932. size32_t RtlPackedIntTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  933. {
  934. char temp[9];
  935. size32_t size = in.readPackedInt(temp);
  936. byte * dest = builder.ensureCapacity(offset + size, nullptr) + offset;
  937. memcpy(dest, temp, size);
  938. return offset + size;
  939. }
  940. void RtlPackedIntTypeInfo::readAhead(IRowPrefetcherSource & in) const
  941. {
  942. in.skipPackedInt();
  943. }
  944. void RtlPackedIntTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  945. {
  946. if (isUnsigned())
  947. rtlUInt8ToStrX(resultLen, result, rtlGetPackedUnsigned(ptr));
  948. else
  949. rtlInt8ToStrX(resultLen, result, rtlGetPackedSigned(ptr));
  950. }
  951. void RtlPackedIntTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  952. {
  953. getString(resultLen, result, ptr);
  954. }
  955. __int64 RtlPackedIntTypeInfo::getInt(const void * ptr) const
  956. {
  957. if (isUnsigned())
  958. return rtlGetPackedUnsigned(ptr);
  959. else
  960. return rtlGetPackedSigned(ptr);
  961. }
  962. double RtlPackedIntTypeInfo::getReal(const void * ptr) const
  963. {
  964. if (isUnsigned())
  965. return (double) rtlGetPackedUnsigned(ptr);
  966. else
  967. return (double) rtlGetPackedSigned(ptr);
  968. }
  969. int RtlPackedIntTypeInfo::compare(const byte * left, const byte * right) const
  970. {
  971. if (isUnsigned())
  972. {
  973. unsigned __int64 leftValue = rtlGetPackedUnsigned(left);
  974. unsigned __int64 rightValue = rtlGetPackedUnsigned(right);
  975. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  976. }
  977. else
  978. {
  979. __int64 leftValue = rtlGetPackedSigned(left);
  980. __int64 rightValue = rtlGetPackedSigned(right);
  981. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  982. }
  983. }
  984. unsigned RtlPackedIntTypeInfo::hash(const byte *self, unsigned inhash) const
  985. {
  986. __int64 val = getInt(self);
  987. return rtlHash32Data8(&val, inhash);
  988. }
  989. //-------------------------------------------------------------------------------------------------------------------
  990. size32_t RtlStringTypeInfo::getMinSize() const
  991. {
  992. if (isFixedSize())
  993. return length;
  994. return sizeof(size32_t);
  995. }
  996. size32_t RtlStringTypeInfo::size(const byte * self, const byte * selfrow) const
  997. {
  998. if (isFixedSize())
  999. return length;
  1000. return sizeof(size32_t) + rtlReadSize32t(self);
  1001. }
  1002. size32_t RtlStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1003. {
  1004. size32_t size;
  1005. rtlDataAttr value;
  1006. source.getStringResult(field, size, value.refstr());
  1007. return buildString(builder, offset, field, size, value.getstr());
  1008. }
  1009. size32_t RtlStringTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  1010. {
  1011. if (!isFixedSize())
  1012. {
  1013. builder.ensureCapacity(offset+size+sizeof(size32_t), queryName(field));
  1014. byte *dest = builder.getSelf()+offset;
  1015. rtlWriteInt4(dest, size);
  1016. // NOTE - it has been the subject of debate whether we should convert the incoming data to EBCDIC, or expect the IFieldSource to have already returned ebcdic
  1017. // In order to be symmetrical with the passing of ecl data to a IFieldProcessor the former interpretation is preferred.
  1018. // Expecting source.getStringResult to somehow "know" that EBCDIC was expected seems odd.
  1019. if (isEbcdic())
  1020. rtlStrToEStr(size, (char *) dest+sizeof(size32_t), size, (char *)value);
  1021. else
  1022. memcpy(dest+sizeof(size32_t), value, size);
  1023. offset += size+sizeof(size32_t);
  1024. }
  1025. else
  1026. {
  1027. builder.ensureCapacity(offset+length, queryName(field));
  1028. byte *dest = builder.getSelf()+offset;
  1029. if (isEbcdic())
  1030. rtlStrToEStr(length, (char *) dest, size, (char *) value);
  1031. else
  1032. rtlStrToStr(length, dest, size, value);
  1033. offset += length;
  1034. }
  1035. return offset;
  1036. }
  1037. size32_t RtlStringTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t codepoints, const char *value) const
  1038. {
  1039. if (isEbcdic())
  1040. return buildUtf8ViaString(builder, offset, field, codepoints, value);
  1041. if (!isFixedSize())
  1042. {
  1043. builder.ensureCapacity(offset+codepoints+sizeof(size32_t), queryName(field));
  1044. char *dest = (char *) builder.getSelf()+offset;
  1045. rtlWriteInt4(dest, codepoints);
  1046. rtlUtf8ToStr(codepoints, dest+sizeof(size32_t), codepoints, value);
  1047. offset += codepoints+sizeof(size32_t);
  1048. }
  1049. else
  1050. {
  1051. builder.ensureCapacity(offset+length, queryName(field));
  1052. char *dest = (char *) builder.getSelf()+offset;
  1053. rtlUtf8ToStr(length, dest, codepoints, value);
  1054. offset += length;
  1055. }
  1056. return offset;
  1057. }
  1058. size32_t RtlStringTypeInfo::buildNull(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field) const
  1059. {
  1060. if (field->initializer || !isFixedSize())
  1061. return RtlTypeInfoBase::buildNull(builder, offset, field);
  1062. else
  1063. {
  1064. builder.ensureCapacity(offset+length, queryName(field));
  1065. memset(builder.getSelf()+offset, isEbcdic() ? 0x40 : ' ', length);
  1066. return offset + length;
  1067. }
  1068. }
  1069. size32_t RtlStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1070. {
  1071. const char * str = reinterpret_cast<const char *>(self);
  1072. unsigned thisLength;
  1073. unsigned thisSize;
  1074. if (isFixedSize())
  1075. {
  1076. thisLength = length;
  1077. thisSize = thisLength;
  1078. }
  1079. else
  1080. {
  1081. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1082. thisLength = rtlReadSize32t(self);
  1083. thisSize = sizeof(size32_t) + thisLength;
  1084. }
  1085. if (isEbcdic())
  1086. {
  1087. unsigned lenAscii;
  1088. rtlDataAttr ascii;
  1089. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  1090. target.processString(lenAscii, ascii.getstr(), field);
  1091. }
  1092. else
  1093. {
  1094. target.processString(thisLength, str, field);
  1095. }
  1096. return thisSize;
  1097. }
  1098. size32_t RtlStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1099. {
  1100. const char * str = reinterpret_cast<const char *>(self);
  1101. unsigned thisLength;
  1102. unsigned thisSize;
  1103. if (isFixedSize())
  1104. {
  1105. thisLength = length;
  1106. thisSize = thisLength;
  1107. }
  1108. else
  1109. {
  1110. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1111. thisLength = rtlReadSize32t(self);
  1112. thisSize = sizeof(size32_t) + thisLength;
  1113. }
  1114. if (isEbcdic())
  1115. {
  1116. unsigned lenAscii;
  1117. rtlDataAttr ascii;
  1118. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  1119. target.outputString(lenAscii, ascii.getstr(), queryScalarXPath(field));
  1120. }
  1121. else
  1122. {
  1123. target.outputString(thisLength, str, queryScalarXPath(field));
  1124. }
  1125. return thisSize;
  1126. }
  1127. size32_t RtlStringTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  1128. {
  1129. if (isFixedSize())
  1130. {
  1131. size32_t size = length;
  1132. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  1133. in.read(size, dest);
  1134. offset += size;
  1135. }
  1136. else
  1137. {
  1138. size32_t size = in.readSize();
  1139. byte * dest = builder.ensureCapacity(offset+sizeof(size32_t)+size, nullptr) + offset;
  1140. rtlWriteSize32t(dest, size);
  1141. in.read(size, dest + sizeof(size32_t));
  1142. offset += sizeof(size32_t)+size;
  1143. }
  1144. return offset;
  1145. }
  1146. void RtlStringTypeInfo::readAhead(IRowPrefetcherSource & in) const
  1147. {
  1148. if (isFixedSize())
  1149. {
  1150. in.skip(length);
  1151. }
  1152. else
  1153. {
  1154. size32_t thisLength = in.readSize();
  1155. in.skip(thisLength);
  1156. }
  1157. }
  1158. void RtlStringTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  1159. {
  1160. if (isFixedSize())
  1161. {
  1162. if (isEbcdic())
  1163. return rtlEStrToStrX(resultLen, result, length, (const char *)ptr);
  1164. else
  1165. return rtlStrToStrX(resultLen, result, length, (const char *)ptr);
  1166. }
  1167. else
  1168. {
  1169. size32_t len = rtlReadSize32t(ptr);
  1170. if (isEbcdic())
  1171. return rtlEStrToStrX(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1172. else
  1173. return rtlStrToStrX(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1174. }
  1175. }
  1176. void RtlStringTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  1177. {
  1178. if (isEbcdic())
  1179. {
  1180. getUtf8ViaString(resultLen, result, ptr);
  1181. return;
  1182. }
  1183. if (isFixedSize())
  1184. {
  1185. return rtlStrToUtf8X(resultLen, result, length, (const char *)ptr);
  1186. }
  1187. else
  1188. {
  1189. size32_t len = rtlReadSize32t(ptr);
  1190. return rtlStrToUtf8X(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1191. }
  1192. }
  1193. __int64 RtlStringTypeInfo::getInt(const void * ptr) const
  1194. {
  1195. //Utf8 output is the same as string output, so avoid the intermediate translation
  1196. if (isFixedSize())
  1197. return rtlStrToInt8(length, (const char *)ptr);
  1198. size32_t len = rtlReadSize32t(ptr);
  1199. return rtlStrToInt8(len, (const char *)ptr + sizeof(size32_t));
  1200. }
  1201. int RtlStringTypeInfo::compare(const byte * left, const byte * right) const
  1202. {
  1203. if (isFixedSize())
  1204. return memcmp(left, right, length);
  1205. else if (isEbcdic())
  1206. {
  1207. // Logically this should be
  1208. // if (isFixedSize())
  1209. // return rtlCompareEStrEStr(length, (const char *)left, length, (const char *)right);
  1210. // but that's the same as a memcmp if lengths match
  1211. size32_t lenLeft = rtlReadSize32t(left);
  1212. size32_t lenRight = rtlReadSize32t(right);
  1213. return rtlCompareEStrEStr(lenLeft, (const char *)left + sizeof(size32_t), lenRight, (const char *)right + sizeof(size32_t));
  1214. }
  1215. else
  1216. {
  1217. // Logically this should be
  1218. // if (isFixedSize())
  1219. // return rtlCompareStrStr(length, (const char *)left, length, (const char *)right); // Actually this is a memcmp
  1220. // but that's the same as a memcmp if lengths match
  1221. size32_t lenLeft = rtlReadSize32t(left);
  1222. size32_t lenRight = rtlReadSize32t(right);
  1223. return rtlCompareStrStr(lenLeft, (const char *)left + sizeof(size32_t), lenRight, (const char *)right + sizeof(size32_t));
  1224. }
  1225. }
  1226. int RtlStringTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  1227. {
  1228. if (isFixedSize())
  1229. {
  1230. lenLeft = std::min(lenLeft, length);
  1231. lenRight = std::min(lenRight, length);
  1232. if (isEbcdic())
  1233. return rtlCompareEStrEStr(lenLeft, (const char *)left, lenRight, (const char *)right);
  1234. else
  1235. return rtlCompareStrStr(lenLeft, (const char *)left, lenRight, (const char *)right);
  1236. }
  1237. else
  1238. {
  1239. size32_t maxLeft = rtlReadSize32t(left);
  1240. size32_t maxRight = rtlReadSize32t(right);
  1241. if (isEbcdic())
  1242. return rtlCompareEStrEStr(std::min(lenLeft, maxLeft), (const char *)left + sizeof(size32_t), std::min(lenRight, maxRight), (const char *)right + sizeof(size32_t));
  1243. else
  1244. return rtlCompareStrStr(std::min(lenLeft, maxLeft), (const char *)left + sizeof(size32_t), std::min(lenRight, maxRight), (const char *)right + sizeof(size32_t));
  1245. }
  1246. }
  1247. bool RtlStringTypeInfo::canMemCmp() const
  1248. {
  1249. return isFixedSize();
  1250. }
  1251. unsigned RtlStringTypeInfo::hash(const byte * self, unsigned inhash) const
  1252. {
  1253. size32_t len;
  1254. if (isFixedSize())
  1255. len = length;
  1256. else
  1257. {
  1258. len = rtlReadSize32t(self);
  1259. self += sizeof(size32_t);
  1260. }
  1261. return rtlHash32Data(rtlTrimStrLen(len, (const char *) self), self, inhash);
  1262. }
  1263. bool RtlStringTypeInfo::canExtend(char &fillChar) const
  1264. {
  1265. if (isFixedSize())
  1266. {
  1267. fillChar = isEbcdic() ? 0x40 : ' ';
  1268. return true;
  1269. }
  1270. return false;
  1271. }
  1272. //-------------------------------------------------------------------------------------------------------------------
  1273. size32_t RtlDataTypeInfo::getMinSize() const
  1274. {
  1275. if (isFixedSize())
  1276. return length;
  1277. return sizeof(size32_t);
  1278. }
  1279. size32_t RtlDataTypeInfo::size(const byte * self, const byte * selfrow) const
  1280. {
  1281. if (isFixedSize())
  1282. return length;
  1283. return sizeof(size32_t) + rtlReadSize32t(self);
  1284. }
  1285. size32_t RtlDataTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1286. {
  1287. size32_t size;
  1288. rtlDataAttr value;
  1289. source.getDataResult(field, size, value.refdata());
  1290. return buildString(builder, offset, field, size, value.getstr());
  1291. }
  1292. size32_t RtlDataTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  1293. {
  1294. if (!isFixedSize())
  1295. {
  1296. builder.ensureCapacity(offset+size+sizeof(size32_t), queryName(field));
  1297. byte *dest = builder.getSelf()+offset;
  1298. rtlWriteInt4(dest, size);
  1299. memcpy(dest+sizeof(size32_t), value, size);
  1300. offset += size+sizeof(size32_t);
  1301. }
  1302. else
  1303. {
  1304. builder.ensureCapacity(offset+length, queryName(field));
  1305. byte *dest = builder.getSelf()+offset;
  1306. rtlDataToData(length, dest, size, value);
  1307. offset += length;
  1308. }
  1309. return offset;
  1310. }
  1311. size32_t RtlDataTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t codepoints, const char *value) const
  1312. {
  1313. return buildUtf8ViaString(builder, offset, field, codepoints, value);
  1314. }
  1315. size32_t RtlDataTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1316. {
  1317. const char * str = reinterpret_cast<const char *>(self);
  1318. unsigned thisLength;
  1319. unsigned thisSize;
  1320. if (isFixedSize())
  1321. {
  1322. thisLength = length;
  1323. thisSize = thisLength;
  1324. }
  1325. else
  1326. {
  1327. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1328. thisLength = rtlReadSize32t(self);
  1329. thisSize = sizeof(size32_t) + thisLength;
  1330. }
  1331. target.processData(thisLength, str, field);
  1332. return thisSize;
  1333. }
  1334. size32_t RtlDataTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1335. {
  1336. const char * str = reinterpret_cast<const char *>(self);
  1337. unsigned thisLength;
  1338. unsigned thisSize;
  1339. if (isFixedSize())
  1340. {
  1341. thisLength = length;
  1342. thisSize = thisLength;
  1343. }
  1344. else
  1345. {
  1346. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1347. thisLength = rtlReadSize32t(self);
  1348. thisSize = sizeof(size32_t) + thisLength;
  1349. }
  1350. target.outputData(thisLength, str, queryScalarXPath(field));
  1351. return thisSize;
  1352. }
  1353. size32_t RtlDataTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  1354. {
  1355. if (isFixedSize())
  1356. {
  1357. size32_t size = length;
  1358. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  1359. in.read(size, dest);
  1360. offset += size;
  1361. }
  1362. else
  1363. {
  1364. size32_t size = in.readSize();
  1365. byte * dest = builder.ensureCapacity(offset+sizeof(size32_t)+size, nullptr) + offset;
  1366. rtlWriteSize32t(dest, size);
  1367. in.read(size, dest + sizeof(size32_t));
  1368. offset += sizeof(size32_t)+size;
  1369. }
  1370. return offset;
  1371. }
  1372. void RtlDataTypeInfo::readAhead(IRowPrefetcherSource & in) const
  1373. {
  1374. if (isFixedSize())
  1375. {
  1376. in.skip(length);
  1377. }
  1378. else
  1379. {
  1380. size32_t thisLength = in.readSize();
  1381. in.skip(thisLength);
  1382. }
  1383. }
  1384. void RtlDataTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  1385. {
  1386. if (isFixedSize())
  1387. {
  1388. return rtlStrToStrX(resultLen, result, length, (const char *)ptr);
  1389. }
  1390. else
  1391. {
  1392. size32_t len = rtlReadSize32t(ptr);
  1393. return rtlStrToStrX(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1394. }
  1395. }
  1396. void RtlDataTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  1397. {
  1398. if (isFixedSize())
  1399. {
  1400. return rtlStrToUtf8X(resultLen, result, length, (const char *)ptr);
  1401. }
  1402. else
  1403. {
  1404. size32_t len = rtlReadSize32t(ptr);
  1405. return rtlStrToUtf8X(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1406. }
  1407. }
  1408. __int64 RtlDataTypeInfo::getInt(const void * ptr) const
  1409. {
  1410. //Utf8 output is the same as string output, so avoid the intermediate translation
  1411. if (isFixedSize())
  1412. return rtlStrToInt8(length, (const char *)ptr);
  1413. size32_t len = rtlReadSize32t(ptr);
  1414. return rtlStrToInt8(len, (const char *)ptr + sizeof(size32_t));
  1415. }
  1416. int RtlDataTypeInfo::compare(const byte * left, const byte * right) const
  1417. {
  1418. if (isFixedSize())
  1419. // Logically this should be return rtlCompareDataData(length, (const char *)left, length, (const char *)right);
  1420. // but that acts as a memcmp if lengths match
  1421. return memcmp(left, right, length);
  1422. size32_t lenLeft = rtlReadSize32t(left);
  1423. size32_t lenRight = rtlReadSize32t(right);
  1424. return rtlCompareDataData(lenLeft, (const char *)left + sizeof(size32_t), lenRight, (const char *)right + sizeof(size32_t));
  1425. }
  1426. int RtlDataTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  1427. {
  1428. if (isFixedSize())
  1429. {
  1430. lenLeft = std::min(lenLeft, length);
  1431. lenRight = std::min(lenRight, length);
  1432. return rtlCompareDataData(lenLeft, (const char *)left, lenRight, (const char *)right);
  1433. }
  1434. else
  1435. {
  1436. size32_t maxLeft = rtlReadSize32t(left);
  1437. size32_t maxRight = rtlReadSize32t(right);
  1438. lenLeft = std::min(lenLeft, maxLeft);
  1439. lenRight = std::min(lenRight, maxRight);
  1440. return rtlCompareDataData(lenLeft, (const char *)left + sizeof(size32_t), lenRight, (const char *)right + sizeof(size32_t));
  1441. }
  1442. }
  1443. bool RtlDataTypeInfo::canMemCmp() const
  1444. {
  1445. return isFixedSize();
  1446. }
  1447. unsigned RtlDataTypeInfo::hash(const byte *self, unsigned inhash) const
  1448. {
  1449. size32_t len;
  1450. if (isFixedSize())
  1451. len = length;
  1452. else
  1453. {
  1454. len = rtlReadSize32t(self);
  1455. self += sizeof(size32_t);
  1456. }
  1457. return rtlHash32Data(len, self, inhash);
  1458. }
  1459. bool RtlDataTypeInfo::canExtend(char &fillChar) const
  1460. {
  1461. if (isFixedSize())
  1462. {
  1463. fillChar = 0;
  1464. return true;
  1465. }
  1466. return false;
  1467. }
  1468. //-------------------------------------------------------------------------------------------------------------------
  1469. size32_t RtlVarStringTypeInfo::getMinSize() const
  1470. {
  1471. if (isFixedSize())
  1472. return length+1;
  1473. return 1;
  1474. }
  1475. size32_t RtlVarStringTypeInfo::size(const byte * self, const byte * selfrow) const
  1476. {
  1477. if (isFixedSize())
  1478. return length + 1;
  1479. const char * str = reinterpret_cast<const char *>(self);
  1480. return (size32_t)strlen(str)+1;
  1481. }
  1482. size32_t RtlVarStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1483. {
  1484. size32_t size;
  1485. rtlDataAttr value;
  1486. source.getStringResult(field, size, value.refstr());
  1487. return buildString(builder, offset, field, size, value.getstr());
  1488. }
  1489. size32_t RtlVarStringTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  1490. {
  1491. if (!isFixedSize())
  1492. {
  1493. builder.ensureCapacity(offset+size+1, queryName(field));
  1494. // See notes re EBCDIC conversion in RtlStringTypeInfo code
  1495. byte *dest = builder.getSelf()+offset;
  1496. if (isEbcdic())
  1497. rtlStrToEStr(size, (char *) dest, size, (char *)value);
  1498. else
  1499. memcpy(dest, value, size);
  1500. dest[size] = '\0';
  1501. offset += size+1;
  1502. }
  1503. else
  1504. {
  1505. builder.ensureCapacity(offset+length+1, queryName(field));
  1506. byte *dest = builder.getSelf()+offset;
  1507. if (isEbcdic())
  1508. rtlEStrToVStr(length+1, dest, size, value);
  1509. else
  1510. rtlStrToVStr(length+1, dest, size, value);
  1511. offset += length+1;
  1512. }
  1513. return offset;
  1514. }
  1515. size32_t RtlVarStringTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t codepoints, const char *value) const
  1516. {
  1517. return buildUtf8ViaString(builder, offset, field, codepoints, value);
  1518. }
  1519. size32_t RtlVarStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1520. {
  1521. const char * str = reinterpret_cast<const char *>(self);
  1522. unsigned thisLength = (size32_t)strlen(str);
  1523. unsigned thisSize;
  1524. if (isFixedSize())
  1525. thisSize = length+1;
  1526. else
  1527. thisSize = thisLength+1;
  1528. if (isEbcdic())
  1529. {
  1530. unsigned lenAscii;
  1531. rtlDataAttr ascii;
  1532. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  1533. target.processString(lenAscii, ascii.getstr(), field);
  1534. }
  1535. else
  1536. target.processString(thisLength, str, field);
  1537. return thisSize;
  1538. }
  1539. size32_t RtlVarStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1540. {
  1541. const char * str = reinterpret_cast<const char *>(self);
  1542. unsigned thisLength = (size32_t)strlen(str);
  1543. unsigned thisSize;
  1544. if (isFixedSize())
  1545. thisSize = length+1;
  1546. else
  1547. thisSize = thisLength+1;
  1548. if (isEbcdic())
  1549. {
  1550. unsigned lenAscii;
  1551. rtlDataAttr ascii;
  1552. rtlEStrToStrX(lenAscii, ascii.refstr(), thisLength, str);
  1553. target.outputString(lenAscii, ascii.getstr(), queryScalarXPath(field));
  1554. }
  1555. else
  1556. target.outputString(thisLength, str, queryScalarXPath(field));
  1557. return thisSize;
  1558. }
  1559. size32_t RtlVarStringTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  1560. {
  1561. if (isFixedSize())
  1562. {
  1563. size32_t size = length+1;
  1564. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  1565. in.read(size, dest);
  1566. return offset + size;
  1567. }
  1568. else
  1569. return offset + in.readVStr(builder, offset, offset);
  1570. }
  1571. void RtlVarStringTypeInfo::readAhead(IRowPrefetcherSource & in) const
  1572. {
  1573. if (isFixedSize())
  1574. {
  1575. in.skip(length+1);
  1576. }
  1577. else
  1578. {
  1579. in.skipVStr();
  1580. }
  1581. }
  1582. void RtlVarStringTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  1583. {
  1584. const char * str = (const char *)ptr;
  1585. return rtlStrToStrX(resultLen, result, strlen(str), str);
  1586. }
  1587. void RtlVarStringTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  1588. {
  1589. const char * str = (const char *)ptr;
  1590. return rtlStrToUtf8X(resultLen, result, strlen(str), str);
  1591. }
  1592. __int64 RtlVarStringTypeInfo::getInt(const void * ptr) const
  1593. {
  1594. const char * str = (const char *)ptr;
  1595. return rtlVStrToInt8(str);
  1596. }
  1597. int RtlVarStringTypeInfo::compare(const byte * left, const byte * right) const
  1598. {
  1599. if (isEbcdic())
  1600. {
  1601. const char * leftValue = (const char *)left;
  1602. const char * rightValue = (const char *)right;
  1603. return rtlCompareEStrEStr(strlen(leftValue), leftValue, strlen(rightValue), rightValue);
  1604. }
  1605. return rtlCompareVStrVStr((const char *)left, (const char *)right);
  1606. }
  1607. int RtlVarStringTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  1608. {
  1609. size32_t maxLeft = strlen((const char *)left);
  1610. size32_t maxRight = strlen((const char *)right);
  1611. lenLeft = std::min(lenLeft, maxLeft);
  1612. lenRight = std::min(lenRight, maxRight);
  1613. if (isEbcdic())
  1614. return rtlCompareEStrEStr(lenLeft, (const char *)left, lenRight, (const char *)right);
  1615. else
  1616. return rtlCompareStrStr(lenLeft, (const char *)left, lenRight, (const char *)right);
  1617. }
  1618. void RtlVarStringTypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  1619. {
  1620. //Do not support substring matching or non-exclusive bounds.
  1621. //They could possibly be implemented by copying a varstring at maxlength padded with spaces.
  1622. if ((subLength != MatchFullString) || !inclusive)
  1623. UNIMPLEMENTED_X("RtlVarStringTypeInfo::setBound");
  1624. RtlTypeInfoBase::setBound(buffer, value, subLength, fill, inclusive);
  1625. }
  1626. unsigned RtlVarStringTypeInfo::hash(const byte * self, unsigned inhash) const
  1627. {
  1628. return rtlHash32VStr((const char *) self, inhash);
  1629. }
  1630. bool RtlVarStringTypeInfo::canExtend(char &fillChar) const
  1631. {
  1632. if (isFixedSize())
  1633. {
  1634. fillChar = 0;
  1635. return true;
  1636. }
  1637. return false;
  1638. }
  1639. //-------------------------------------------------------------------------------------------------------------------
  1640. size32_t RtlQStringTypeInfo::getMinSize() const
  1641. {
  1642. if (isFixedSize())
  1643. return rtlQStrSize(length);
  1644. return sizeof(size32_t);
  1645. }
  1646. size32_t RtlQStringTypeInfo::size(const byte * self, const byte * selfrow) const
  1647. {
  1648. if (isFixedSize())
  1649. return rtlQStrSize(length);
  1650. return sizeof(size32_t) + rtlQStrSize(rtlReadSize32t(self));
  1651. }
  1652. size32_t RtlQStringTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1653. {
  1654. size32_t size;
  1655. rtlDataAttr value;
  1656. source.getStringResult(field, size, value.refstr());
  1657. return buildString(builder, offset, field, size, value.getstr());
  1658. }
  1659. size32_t RtlQStringTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  1660. {
  1661. if (!isFixedSize())
  1662. {
  1663. size32_t sizeInBytes = rtlQStrSize(size) + sizeof(size32_t);
  1664. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  1665. byte *dest = builder.getSelf()+offset;
  1666. rtlWriteInt4(dest, size);
  1667. rtlStrToQStr(size, (char *) dest+sizeof(size32_t), size, value);
  1668. offset += sizeInBytes;
  1669. }
  1670. else
  1671. {
  1672. size32_t sizeInBytes = rtlQStrSize(length);
  1673. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  1674. byte *dest = builder.getSelf()+offset;
  1675. rtlStrToQStr(length, (char *) dest, size, value);
  1676. offset += sizeInBytes;
  1677. }
  1678. return offset;
  1679. }
  1680. size32_t RtlQStringTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t codepoints, const char *value) const
  1681. {
  1682. return buildUtf8ViaString(builder, offset, field, codepoints, value);
  1683. }
  1684. size32_t RtlQStringTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1685. {
  1686. const char * str = reinterpret_cast<const char *>(self);
  1687. unsigned thisLength;
  1688. unsigned thisSize;
  1689. if (isFixedSize())
  1690. {
  1691. thisLength = length;
  1692. thisSize = rtlQStrSize(thisLength);
  1693. }
  1694. else
  1695. {
  1696. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1697. thisLength = rtlReadSize32t(self);
  1698. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  1699. }
  1700. target.processQString(thisLength, str, field);
  1701. return thisSize;
  1702. }
  1703. size32_t RtlQStringTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1704. {
  1705. const char * str = reinterpret_cast<const char *>(self);
  1706. unsigned thisLength;
  1707. unsigned thisSize;
  1708. if (isFixedSize())
  1709. {
  1710. thisLength = length;
  1711. thisSize = rtlQStrSize(thisLength);
  1712. }
  1713. else
  1714. {
  1715. str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  1716. thisLength = rtlReadSize32t(self);
  1717. thisSize = sizeof(size32_t) + rtlQStrSize(thisLength);
  1718. }
  1719. target.outputQString(thisLength, str, queryScalarXPath(field));
  1720. return thisSize;
  1721. }
  1722. size32_t RtlQStringTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  1723. {
  1724. if (isFixedSize())
  1725. {
  1726. size32_t size = rtlQStrSize(length);
  1727. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  1728. in.read(size, dest);
  1729. offset += size;
  1730. }
  1731. else
  1732. {
  1733. size32_t thisLength = in.readSize();
  1734. size32_t size = rtlQStrSize(thisLength);
  1735. byte * dest = builder.ensureCapacity(offset+sizeof(size32_t)+size, nullptr) + offset;
  1736. rtlWriteSize32t(dest, thisLength);
  1737. in.read(size, dest + sizeof(size32_t));
  1738. offset += sizeof(size32_t)+size;
  1739. }
  1740. return offset;
  1741. }
  1742. void RtlQStringTypeInfo::readAhead(IRowPrefetcherSource & in) const
  1743. {
  1744. if (isFixedSize())
  1745. {
  1746. size32_t size = rtlQStrSize(length);
  1747. in.skip(size);
  1748. }
  1749. else
  1750. {
  1751. size32_t thisLength = in.readSize();
  1752. size32_t size = rtlQStrSize(thisLength);
  1753. in.skip(size);
  1754. }
  1755. }
  1756. void RtlQStringTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  1757. {
  1758. if (isFixedSize())
  1759. {
  1760. return rtlQStrToStrX(resultLen, result, length, (const char *)ptr);
  1761. }
  1762. else
  1763. {
  1764. size32_t len = rtlReadSize32t(ptr);
  1765. return rtlQStrToStrX(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  1766. }
  1767. }
  1768. void RtlQStringTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  1769. {
  1770. //NOTE: QStrings cannot contain non-string characters, so converting to str is the same as utf8
  1771. getString(resultLen, result, ptr);
  1772. }
  1773. __int64 RtlQStringTypeInfo::getInt(const void * ptr) const
  1774. {
  1775. size32_t lenTemp;
  1776. rtlDataAttr temp;
  1777. getUtf8(lenTemp, temp.refstr(), ptr);
  1778. return rtlStrToInt8(lenTemp, temp.getstr());
  1779. }
  1780. int RtlQStringTypeInfo::compare(const byte * left, const byte * right) const
  1781. {
  1782. if (isFixedSize())
  1783. // Logically this should be return rtlCompareQStrQStr(length, left, length, right);
  1784. // but that acts as a memcmp if lengths match
  1785. return memcmp(left, right, rtlQStrSize(length));
  1786. size32_t lenLeft = rtlReadSize32t(left);
  1787. size32_t lenRight = rtlReadSize32t(right);
  1788. return rtlCompareQStrQStr(lenLeft, left + sizeof(size32_t), lenRight, right + sizeof(size32_t));
  1789. }
  1790. int RtlQStringTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  1791. {
  1792. if (isFixedSize())
  1793. {
  1794. lenLeft = std::min(lenLeft, length);
  1795. lenRight = std::min(lenRight, length);
  1796. return rtlSafeCompareQStrQStr(lenLeft, (const char *)left, lenRight, (const char *)right);
  1797. }
  1798. else
  1799. {
  1800. size32_t maxLeft = rtlReadSize32t(left);
  1801. size32_t maxRight = rtlReadSize32t(right);
  1802. lenLeft = std::min(lenLeft, maxLeft);
  1803. lenRight = std::min(lenRight, maxRight);
  1804. return rtlSafeCompareQStrQStr(lenLeft, (const char *)left + sizeof(size32_t), lenRight, (const char *)right + sizeof(size32_t));
  1805. }
  1806. }
  1807. void RtlQStringTypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  1808. {
  1809. byte *dst = (byte *) buffer;
  1810. size32_t minSize = getMinSize();
  1811. if (value)
  1812. {
  1813. //Eventually the comparison code should not ever use non inclusive, or compare characters after subLength
  1814. //Until then this code needs to fill the rest of the string with the correct fill character and
  1815. //increment the appropriate qcharacter
  1816. size32_t copyLen;
  1817. if (isFixedSize())
  1818. {
  1819. //For a substring comparison, the bytes after subLength will eventually never be compared - but index currently does
  1820. copyLen = std::min(length, subLength);
  1821. rtlQStrToQStr(length, (char *)buffer, copyLen, (const char *)value);
  1822. if (fill != 0)
  1823. {
  1824. for (unsigned i=copyLen; i < length; i++)
  1825. setQChar(dst, i, 0x3F);
  1826. }
  1827. else if (copyLen != length)
  1828. setQChar(dst, copyLen, 0); // work around a bug copying qstrings of the same size, but different lengths
  1829. }
  1830. else
  1831. {
  1832. copyLen = std::min(rtlReadSize32t(value), subLength);
  1833. size32_t copySize = size(value, nullptr);
  1834. memcpy(dst, value, copySize);
  1835. }
  1836. if (!inclusive)
  1837. {
  1838. if (fill == 0)
  1839. incrementQString(dst, copyLen);
  1840. else
  1841. decrementQString(dst, copyLen);
  1842. }
  1843. }
  1844. else
  1845. {
  1846. assertex((fill == 0) || isFixedSize());
  1847. memset(dst, fill, minSize);
  1848. }
  1849. }
  1850. bool RtlQStringTypeInfo::canMemCmp() const
  1851. {
  1852. return isFixedSize();
  1853. }
  1854. unsigned RtlQStringTypeInfo::hash(const byte * self, unsigned inhash) const
  1855. {
  1856. rtlDataAttr val;
  1857. unsigned len;
  1858. getString(len, val.refstr(), self);
  1859. return rtlHash32Data(rtlTrimStrLen(len, val.getstr()), val.getstr(), inhash);
  1860. }
  1861. bool RtlQStringTypeInfo::canExtend(char &fillChar) const
  1862. {
  1863. if (isFixedSize())
  1864. {
  1865. fillChar = 0;
  1866. return true;
  1867. }
  1868. return false;
  1869. }
  1870. //-------------------------------------------------------------------------------------------------------------------
  1871. size32_t RtlDecimalTypeInfo::calcSize() const
  1872. {
  1873. if (isUnsigned())
  1874. return (getDecimalDigits()+1)/2;
  1875. return (getDecimalDigits()+2)/2;
  1876. }
  1877. size32_t RtlDecimalTypeInfo::getMinSize() const
  1878. {
  1879. return calcSize();
  1880. }
  1881. size32_t RtlDecimalTypeInfo::size(const byte * self, const byte * selfrow) const
  1882. {
  1883. return calcSize();
  1884. }
  1885. size32_t RtlDecimalTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1886. {
  1887. Decimal value;
  1888. source.getDecimalResult(field, value);
  1889. size32_t sizeInBytes = calcSize();
  1890. builder.ensureCapacity(sizeInBytes+offset, queryName(field));
  1891. if (isUnsigned())
  1892. value.getUDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1893. else
  1894. value.getDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1895. offset += sizeInBytes;
  1896. return offset;
  1897. }
  1898. size32_t RtlDecimalTypeInfo::buildNull(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field) const
  1899. {
  1900. if (field->initializer)
  1901. return RtlTypeInfoBase::buildNull(builder, offset, field);
  1902. Decimal value;
  1903. size32_t sizeInBytes = calcSize();
  1904. builder.ensureCapacity(sizeInBytes+offset, queryName(field));
  1905. if (isUnsigned())
  1906. value.getUDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1907. else
  1908. value.getDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1909. offset += sizeInBytes;
  1910. return offset;
  1911. }
  1912. size32_t RtlDecimalTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  1913. {
  1914. Decimal dvalue;
  1915. dvalue.setString(len, value);
  1916. size32_t sizeInBytes = calcSize();
  1917. builder.ensureCapacity(sizeInBytes+offset, queryName(field));
  1918. if (isUnsigned())
  1919. dvalue.getUDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1920. else
  1921. dvalue.getDecimal(sizeInBytes, getDecimalPrecision(), builder.getSelf()+offset);
  1922. offset += sizeInBytes;
  1923. return offset;
  1924. }
  1925. size32_t RtlDecimalTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  1926. {
  1927. size32_t size = rtlUtf8Length(len, value);
  1928. return buildString(builder, offset, field, size, value);
  1929. }
  1930. size32_t RtlDecimalTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  1931. {
  1932. size32_t thisSize = calcSize();
  1933. if (isUnsigned())
  1934. target.processUDecimal(self, thisSize, getDecimalPrecision(), field);
  1935. else
  1936. target.processDecimal(self, thisSize, getDecimalPrecision(), field);
  1937. return thisSize;
  1938. }
  1939. size32_t RtlDecimalTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  1940. {
  1941. size32_t thisSize = calcSize();
  1942. if (isUnsigned())
  1943. target.outputUDecimal(self, thisSize, getDecimalPrecision(), queryScalarXPath(field));
  1944. else
  1945. target.outputDecimal(self, thisSize, getDecimalPrecision(), queryScalarXPath(field));
  1946. return thisSize;
  1947. }
  1948. void RtlDecimalTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  1949. {
  1950. Decimal temp;
  1951. size32_t sizeInBytes = calcSize();
  1952. if (isUnsigned())
  1953. temp.setUDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1954. else
  1955. temp.setDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1956. temp.getStringX(resultLen, result);
  1957. }
  1958. void RtlDecimalTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  1959. {
  1960. getString(resultLen, result, ptr);
  1961. }
  1962. __int64 RtlDecimalTypeInfo::getInt(const void * ptr) const
  1963. {
  1964. Decimal temp;
  1965. size32_t sizeInBytes = calcSize();
  1966. if (isUnsigned())
  1967. temp.setUDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1968. else
  1969. temp.setDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1970. return temp.getInt64();
  1971. }
  1972. double RtlDecimalTypeInfo::getReal(const void * ptr) const
  1973. {
  1974. Decimal temp;
  1975. size32_t sizeInBytes = calcSize();
  1976. if (isUnsigned())
  1977. temp.setUDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1978. else
  1979. temp.setDecimal(sizeInBytes, getDecimalPrecision(), ptr);
  1980. return temp.getReal();
  1981. }
  1982. int RtlDecimalTypeInfo::compare(const byte * left, const byte * right) const
  1983. {
  1984. if (isUnsigned())
  1985. return decCompareUDecimal(calcSize(), left, right);
  1986. else
  1987. return decCompareDecimal(calcSize(), left, right);
  1988. }
  1989. unsigned RtlDecimalTypeInfo::hash(const byte * self, unsigned inhash) const
  1990. {
  1991. return rtlHash32Data(calcSize(), self, inhash);
  1992. }
  1993. //-------------------------------------------------------------------------------------------------------------------
  1994. size32_t RtlCharTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  1995. {
  1996. throwUnexpected(); // Can't have a field of type char
  1997. }
  1998. size32_t RtlCharTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  1999. {
  2000. rtlFailUnexpected();
  2001. }
  2002. size32_t RtlCharTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  2003. {
  2004. return buildUtf8ViaString(builder, offset, field, len, value);
  2005. }
  2006. size32_t RtlCharTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2007. {
  2008. const char * str = reinterpret_cast<const char *>(self);
  2009. char c;
  2010. if (isEbcdic())
  2011. rtlEStrToStr(1, &c, 1, str);
  2012. else
  2013. c = *str;
  2014. target.processString(1, &c, field);
  2015. return 1;
  2016. }
  2017. size32_t RtlCharTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  2018. {
  2019. const char * str = reinterpret_cast<const char *>(self);
  2020. char c;
  2021. if (isEbcdic())
  2022. rtlEStrToStr(1, &c, 1, str);
  2023. else
  2024. c = *str;
  2025. target.outputString(1, &c, queryScalarXPath(field));
  2026. return 1;
  2027. }
  2028. void RtlCharTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2029. {
  2030. const char * str = (const char *)ptr;
  2031. return rtlStrToStrX(resultLen, result, 1, str);
  2032. }
  2033. void RtlCharTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2034. {
  2035. const char * str = (const char *)ptr;
  2036. return rtlStrToUtf8X(resultLen, result, 1, str);
  2037. }
  2038. __int64 RtlCharTypeInfo::getInt(const void * ptr) const
  2039. {
  2040. const char * str = (const char *)ptr;
  2041. return rtlStrToInt8(1, str);
  2042. }
  2043. int RtlCharTypeInfo::compare(const byte * left, const byte * right) const
  2044. {
  2045. if (isEbcdic())
  2046. return rtlCompareEStrEStr(1, (const char *)left, 1, (const char *)right);
  2047. else
  2048. return rtlCompareStrStr(1, (const char *)left, 1, (const char *)right);
  2049. }
  2050. unsigned RtlCharTypeInfo::hash(const byte * self, unsigned inhash) const
  2051. {
  2052. return rtlHash32Data(1, self, inhash); // MORE - should we trim?
  2053. }
  2054. //-------------------------------------------------------------------------------------------------------------------
  2055. size32_t RtlUnicodeTypeInfo::getMinSize() const
  2056. {
  2057. if (isFixedSize())
  2058. return length * sizeof(UChar);
  2059. return sizeof(size32_t);
  2060. }
  2061. size32_t RtlUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  2062. {
  2063. if (isFixedSize())
  2064. return length * sizeof(UChar);
  2065. return sizeof(size32_t) + rtlReadSize32t(self) * sizeof(UChar);
  2066. }
  2067. size32_t RtlUnicodeTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  2068. {
  2069. size32_t sizeInChars;
  2070. UChar *value;
  2071. source.getUnicodeResult(field, sizeInChars, value);
  2072. if (!isFixedSize())
  2073. {
  2074. size32_t sizeInBytes = sizeInChars * sizeof(UChar);
  2075. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), queryName(field));
  2076. byte *dest = builder.getSelf()+offset;
  2077. rtlWriteInt4(dest, sizeInChars); // NOTE - in chars!
  2078. memcpy(dest+sizeof(size32_t), value, sizeInBytes);
  2079. offset += sizeInBytes+sizeof(size32_t);
  2080. }
  2081. else
  2082. {
  2083. size32_t sizeInBytes = length * sizeof(UChar);
  2084. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2085. byte *dest = builder.getSelf()+offset;
  2086. rtlUnicodeToUnicode(length, (UChar *) dest, sizeInChars, value);
  2087. offset += sizeInBytes;
  2088. }
  2089. rtlFree(value);
  2090. return offset;
  2091. }
  2092. size32_t RtlUnicodeTypeInfo::buildNull(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field) const
  2093. {
  2094. if (field->initializer || !isFixedSize())
  2095. return RtlTypeInfoBase::buildNull(builder, offset, field);
  2096. else
  2097. {
  2098. size32_t sizeInBytes = length * sizeof(UChar);
  2099. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2100. byte *dest = builder.getSelf()+offset;
  2101. rtlUnicodeToUnicode(length, (UChar *) dest, 0, nullptr);
  2102. return offset + sizeInBytes;
  2103. }
  2104. }
  2105. size32_t RtlUnicodeTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2106. {
  2107. if (!isFixedSize())
  2108. {
  2109. size32_t sizeInBytes = sizeInChars * sizeof(UChar);
  2110. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), queryName(field));
  2111. byte *dest = builder.getSelf()+offset;
  2112. rtlWriteInt4(dest, sizeInChars); // NOTE - in chars!
  2113. rtlUtf8ToUnicode(sizeInChars, (UChar *) (dest+sizeof(size32_t)), sizeInChars, value);
  2114. offset += sizeInBytes+sizeof(size32_t);
  2115. }
  2116. else
  2117. {
  2118. size32_t sizeInBytes = length * sizeof(UChar);
  2119. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2120. byte *dest = builder.getSelf()+offset;
  2121. rtlUtf8ToUnicode(length, (UChar *) dest, sizeInChars, value);
  2122. offset += sizeInBytes;
  2123. }
  2124. return offset;
  2125. }
  2126. size32_t RtlUnicodeTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2127. {
  2128. if (!isFixedSize())
  2129. {
  2130. size32_t sizeInBytes = sizeInChars * sizeof(UChar);
  2131. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), queryName(field));
  2132. byte *dest = builder.getSelf()+offset;
  2133. rtlWriteSize32t(dest, sizeInChars); // NOTE - in chars!
  2134. rtlStrToUnicode(sizeInChars, (UChar *) (dest+sizeof(size32_t)), sizeInChars, value);
  2135. offset += sizeInBytes+sizeof(size32_t);
  2136. }
  2137. else
  2138. {
  2139. size32_t sizeInBytes = length * sizeof(UChar);
  2140. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2141. byte *dest = builder.getSelf()+offset;
  2142. rtlStrToUnicode(length, (UChar *) dest, sizeInChars, value);
  2143. offset += sizeInBytes;
  2144. }
  2145. return offset;
  2146. }
  2147. size32_t RtlUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2148. {
  2149. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  2150. unsigned thisLength;
  2151. unsigned thisSize;
  2152. if (isFixedSize())
  2153. {
  2154. thisLength = length;
  2155. thisSize = thisLength * sizeof(UChar);
  2156. }
  2157. else
  2158. {
  2159. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  2160. thisLength = rtlReadSize32t(self);
  2161. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  2162. }
  2163. target.processUnicode(thisLength, ustr, field);
  2164. return thisSize;
  2165. }
  2166. size32_t RtlUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  2167. {
  2168. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  2169. unsigned thisLength;
  2170. unsigned thisSize;
  2171. if (isFixedSize())
  2172. {
  2173. thisLength = length;
  2174. thisSize = thisLength * sizeof(UChar);
  2175. }
  2176. else
  2177. {
  2178. ustr = reinterpret_cast<const UChar *>(self + sizeof(size32_t));
  2179. thisLength = rtlReadSize32t(self);
  2180. thisSize = sizeof(size32_t) + thisLength * sizeof(UChar);
  2181. }
  2182. target.outputUnicode(thisLength, ustr, queryScalarXPath(field));
  2183. return thisSize;
  2184. }
  2185. size32_t RtlUnicodeTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  2186. {
  2187. if (isFixedSize())
  2188. {
  2189. size32_t size = length * sizeof(UChar);
  2190. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  2191. in.read(size, dest);
  2192. offset += size;
  2193. }
  2194. else
  2195. {
  2196. size32_t thisLength = in.readSize();
  2197. size32_t size = thisLength * sizeof(UChar);
  2198. byte * dest = builder.ensureCapacity(offset+sizeof(size32_t)+size, nullptr) + offset;
  2199. rtlWriteSize32t(dest, thisLength);
  2200. in.read(size, dest + sizeof(size32_t));
  2201. offset += sizeof(size32_t)+size;
  2202. }
  2203. return offset;
  2204. }
  2205. void RtlUnicodeTypeInfo::readAhead(IRowPrefetcherSource & in) const
  2206. {
  2207. if (isFixedSize())
  2208. {
  2209. size32_t size = length * sizeof(UChar);
  2210. in.skip(size);
  2211. }
  2212. else
  2213. {
  2214. size32_t thisLength = in.readSize();
  2215. size32_t size = thisLength * sizeof(UChar);
  2216. in.skip(size);
  2217. }
  2218. }
  2219. void RtlUnicodeTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2220. {
  2221. if (isFixedSize())
  2222. {
  2223. return rtlUnicodeToStrX(resultLen, result, length, (const UChar *)ptr);
  2224. }
  2225. else
  2226. {
  2227. size32_t len = rtlReadSize32t(ptr);
  2228. const char * str = (const char *)ptr + sizeof(size32_t);
  2229. return rtlUnicodeToStrX(resultLen, result, len, (const UChar *)str);
  2230. }
  2231. }
  2232. void RtlUnicodeTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2233. {
  2234. if (isFixedSize())
  2235. {
  2236. return rtlUnicodeToUtf8X(resultLen, result, length, (const UChar *)ptr);
  2237. }
  2238. else
  2239. {
  2240. size32_t len = rtlReadSize32t(ptr);
  2241. const char * str = (const char *)ptr + sizeof(size32_t);
  2242. return rtlUnicodeToUtf8X(resultLen, result, len, (const UChar *)str);
  2243. }
  2244. }
  2245. __int64 RtlUnicodeTypeInfo::getInt(const void * ptr) const
  2246. {
  2247. //Utf8 output is the same as string output, so avoid the intermediate translation
  2248. if (isFixedSize())
  2249. return rtlUnicodeToInt8(length, (const UChar *)ptr);
  2250. size32_t len = rtlReadSize32t(ptr);
  2251. const char * str = (const char *)ptr + sizeof(size32_t);
  2252. return rtlUnicodeToInt8(len, (const UChar *)str);
  2253. }
  2254. int RtlUnicodeTypeInfo::compare(const byte * left, const byte * right) const
  2255. {
  2256. if (isFixedSize())
  2257. return rtlCompareUnicodeUnicode(length, (const UChar *)left, length, (const UChar *)right, locale);
  2258. size32_t lenLeft = rtlReadSize32t(left);
  2259. size32_t lenRight = rtlReadSize32t(right);
  2260. const UChar * valueLeft = reinterpret_cast<const UChar *>(left + sizeof(size32_t));
  2261. const UChar * valueRight = reinterpret_cast<const UChar *>(right + sizeof(size32_t));
  2262. return rtlCompareUnicodeUnicode(lenLeft, valueLeft, lenRight, valueRight, locale);
  2263. }
  2264. int RtlUnicodeTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  2265. {
  2266. if (isFixedSize())
  2267. {
  2268. lenLeft = std::min(lenLeft, length);
  2269. lenRight = std::min(lenRight, length);
  2270. return rtlCompareUnicodeUnicode(lenLeft, (const UChar *)left, lenRight, (const UChar *)right, locale);
  2271. }
  2272. else
  2273. {
  2274. size32_t maxLeft = rtlReadSize32t(left);
  2275. size32_t maxRight = rtlReadSize32t(right);
  2276. lenLeft = std::min(lenLeft, maxLeft);
  2277. lenRight = std::min(lenRight, maxRight);
  2278. return rtlCompareUnicodeUnicode(lenLeft, (const UChar *)(left + sizeof(size32_t)), lenRight, (const UChar *)(right + sizeof(size32_t)), locale);
  2279. }
  2280. }
  2281. void RtlUnicodeTypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  2282. {
  2283. byte *dst = (byte *) buffer;
  2284. size32_t minSize = getMinSize();
  2285. if (value)
  2286. {
  2287. assertex(inclusive);
  2288. if (subLength != MatchFullString)
  2289. {
  2290. if (isFixedSize())
  2291. {
  2292. //Only support a lower bound for unicode substrings - filled with spaces
  2293. assertex(fill == 0);
  2294. //For a substring comparison, the bytes after subLength will eventually never be compared - but index currently does
  2295. size32_t copyLen = std::min(length, subLength);
  2296. rtlUnicodeToUnicode(length, (UChar *)buffer, copyLen, (const UChar *)value);
  2297. }
  2298. else
  2299. {
  2300. size32_t copyLen = std::min(rtlReadSize32t(value), subLength);
  2301. size32_t copySize = copyLen * sizeof(UChar);
  2302. *(size32_t *)buffer = copyLen;
  2303. memcpy(dst + sizeof(size32_t), value + sizeof(size32_t), copySize);
  2304. }
  2305. }
  2306. else
  2307. {
  2308. size32_t copySize = size(value, nullptr);
  2309. memcpy(dst, value, copySize);
  2310. }
  2311. }
  2312. else
  2313. {
  2314. //Generally cannot work for variable length fields - since always possible to create a larger value
  2315. //Default does not work for real types, or variable length signed integers
  2316. assertex(fill == 0);
  2317. memset(dst, fill, minSize);
  2318. }
  2319. }
  2320. unsigned RtlUnicodeTypeInfo::hash(const byte * self, unsigned inhash) const
  2321. {
  2322. size32_t len;
  2323. if (isFixedSize())
  2324. len = length;
  2325. else
  2326. {
  2327. len = rtlReadSize32t(self);
  2328. self += sizeof(size32_t);
  2329. }
  2330. const UChar * uself = reinterpret_cast<const UChar *>(self);
  2331. return rtlHash32Unicode(rtlTrimUnicodeStrLen(len, uself), uself, inhash);
  2332. }
  2333. //-------------------------------------------------------------------------------------------------------------------
  2334. size32_t RtlVarUnicodeTypeInfo::getMinSize() const
  2335. {
  2336. if (isFixedSize())
  2337. return (length+1) * sizeof(UChar);
  2338. return sizeof(UChar);
  2339. }
  2340. size32_t RtlVarUnicodeTypeInfo::size(const byte * self, const byte * selfrow) const
  2341. {
  2342. if (isFixedSize())
  2343. return (length+1) * sizeof(UChar);
  2344. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  2345. return (rtlUnicodeStrlen(ustr)+1) * sizeof(UChar);
  2346. }
  2347. size32_t RtlVarUnicodeTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  2348. {
  2349. size32_t sizeInChars;
  2350. UChar *value;
  2351. source.getUnicodeResult(field, sizeInChars, value);
  2352. if (!isFixedSize())
  2353. {
  2354. size32_t sizeInBytes = (sizeInChars+1) * sizeof(UChar);
  2355. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2356. UChar *dest = (UChar *) (builder.getSelf()+offset);
  2357. memcpy(dest, value, sizeInBytes - sizeof(UChar));
  2358. dest[sizeInChars] = 0;
  2359. offset += sizeInBytes;
  2360. }
  2361. else
  2362. {
  2363. size32_t sizeInBytes = (length+1) * sizeof(UChar);
  2364. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2365. byte *dest = builder.getSelf()+offset;
  2366. rtlUnicodeToVUnicode(length+1, (UChar *) dest, sizeInChars, value);
  2367. offset += sizeInBytes;
  2368. }
  2369. rtlFree(value);
  2370. return offset;
  2371. }
  2372. size32_t RtlVarUnicodeTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2373. {
  2374. size32_t usize;
  2375. rtlDataAttr uvalue;
  2376. rtlUtf8ToUnicodeX(usize, uvalue.refustr(), sizeInChars, value);
  2377. if (!isFixedSize())
  2378. {
  2379. size32_t sizeInBytes = (usize+1) * sizeof(UChar);
  2380. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2381. UChar *dest = (UChar *) (builder.getSelf()+offset);
  2382. memcpy(dest, uvalue.getustr(), sizeInBytes - sizeof(UChar));
  2383. dest[usize] = 0;
  2384. offset += sizeInBytes;
  2385. }
  2386. else
  2387. {
  2388. size32_t sizeInBytes = (length+1) * sizeof(UChar);
  2389. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2390. byte *dest = builder.getSelf()+offset;
  2391. rtlUnicodeToVUnicode(length+1, (UChar *) dest, usize, uvalue.getustr());
  2392. offset += sizeInBytes;
  2393. }
  2394. return offset;
  2395. }
  2396. size32_t RtlVarUnicodeTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2397. {
  2398. size32_t lengthTarget;
  2399. if (!isFixedSize())
  2400. {
  2401. lengthTarget = (sizeInChars + 1);
  2402. }
  2403. else
  2404. {
  2405. lengthTarget = (length + 1);
  2406. }
  2407. size32_t sizeTarget = lengthTarget * sizeof(UChar);
  2408. builder.ensureCapacity(offset+sizeTarget, queryName(field));
  2409. byte *dest = builder.getSelf()+offset;
  2410. rtlStrToVUnicode(lengthTarget, (UChar *) dest, sizeInChars, value);
  2411. offset += sizeTarget;
  2412. return offset;
  2413. }
  2414. size32_t RtlVarUnicodeTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2415. {
  2416. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  2417. unsigned thisLength = rtlUnicodeStrlen(ustr);
  2418. unsigned thisSize;
  2419. if (isFixedSize())
  2420. thisSize = (length + 1) * sizeof(UChar);
  2421. else
  2422. thisSize = (thisLength + 1) * sizeof(UChar);
  2423. target.processUnicode(thisLength, ustr, field);
  2424. return thisSize;
  2425. }
  2426. size32_t RtlVarUnicodeTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  2427. {
  2428. const UChar * ustr = reinterpret_cast<const UChar *>(self);
  2429. unsigned thisLength = rtlUnicodeStrlen(ustr);
  2430. unsigned thisSize;
  2431. if (isFixedSize())
  2432. thisSize = (length + 1) * sizeof(UChar);
  2433. else
  2434. thisSize = (thisLength + 1) * sizeof(UChar);
  2435. target.outputUnicode(thisLength, ustr, queryScalarXPath(field));
  2436. return thisSize;
  2437. }
  2438. size32_t RtlVarUnicodeTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  2439. {
  2440. if (isFixedSize())
  2441. {
  2442. size32_t size = (length+1)*sizeof(UChar);
  2443. byte * dest = builder.ensureCapacity(offset+size, nullptr) + offset;
  2444. in.read(size, dest);
  2445. return offset + size;
  2446. }
  2447. else
  2448. return offset + in.readVUni(builder, offset, offset);
  2449. }
  2450. void RtlVarUnicodeTypeInfo::readAhead(IRowPrefetcherSource & in) const
  2451. {
  2452. if (isFixedSize())
  2453. {
  2454. size32_t size = (length+1)*sizeof(UChar);
  2455. in.skip(size);
  2456. }
  2457. else
  2458. {
  2459. in.skipVUni();
  2460. }
  2461. }
  2462. void RtlVarUnicodeTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2463. {
  2464. const UChar * str = (const UChar *)ptr;
  2465. rtlUnicodeToStrX(resultLen, result, rtlUnicodeStrlen(str), str);
  2466. }
  2467. void RtlVarUnicodeTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2468. {
  2469. const UChar * str = (const UChar *)ptr;
  2470. rtlUnicodeToUtf8X(resultLen, result, rtlUnicodeStrlen(str), str);
  2471. }
  2472. __int64 RtlVarUnicodeTypeInfo::getInt(const void * ptr) const
  2473. {
  2474. const UChar * str = (const UChar *)ptr;
  2475. return rtlUnicodeToInt8(rtlUnicodeStrlen(str), str);
  2476. }
  2477. int RtlVarUnicodeTypeInfo::compare(const byte * left, const byte * right) const
  2478. {
  2479. const UChar * valueLeft = reinterpret_cast<const UChar *>(left);
  2480. const UChar * valueRight = reinterpret_cast<const UChar *>(right);
  2481. size32_t lenLeft = rtlUnicodeStrlen(valueLeft);
  2482. size32_t lenRight = rtlUnicodeStrlen(valueRight);
  2483. return rtlCompareUnicodeUnicode(lenLeft, valueLeft, lenRight, valueRight, locale);
  2484. }
  2485. int RtlVarUnicodeTypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  2486. {
  2487. size32_t maxLeft = rtlUnicodeStrlen((const UChar *)left);
  2488. size32_t maxRight = rtlUnicodeStrlen((const UChar *)right);
  2489. lenLeft = std::min(lenLeft, maxLeft);
  2490. lenRight = std::min(lenRight, maxRight);
  2491. return rtlCompareUnicodeUnicode(lenLeft, (const UChar *)left, lenRight, (const UChar *)right, locale);
  2492. }
  2493. void RtlVarUnicodeTypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  2494. {
  2495. byte *dst = (byte *) buffer;
  2496. size32_t minSize = getMinSize();
  2497. if (value)
  2498. {
  2499. assertex(inclusive);
  2500. if (subLength != MatchFullString)
  2501. {
  2502. UNIMPLEMENTED_X("VarUnicode::setBound(substring)");
  2503. }
  2504. else
  2505. {
  2506. size32_t copySize = size(value, nullptr);
  2507. memcpy(dst, value, copySize);
  2508. }
  2509. }
  2510. else
  2511. {
  2512. //Generally cannot work for variable length fields - since always possible to create a larger value
  2513. //Default does not work for real types, or variable length signed integers
  2514. assertex(fill == 0);
  2515. memset(dst, fill, minSize);
  2516. }
  2517. }
  2518. unsigned RtlVarUnicodeTypeInfo::hash(const byte * _self, unsigned inhash) const
  2519. {
  2520. const UChar * self = reinterpret_cast<const UChar *>(_self);
  2521. return rtlHash32VUnicode(self, inhash);
  2522. }
  2523. //-------------------------------------------------------------------------------------------------------------------
  2524. size32_t RtlUtf8TypeInfo::getMinSize() const
  2525. {
  2526. return sizeof(size32_t);
  2527. }
  2528. size32_t RtlUtf8TypeInfo::size(const byte * self, const byte * selfrow) const
  2529. {
  2530. assertex(!isFixedSize());
  2531. return sizeof(size32_t) + rtlUtf8Size(rtlReadSize32t(self), self+sizeof(unsigned));
  2532. }
  2533. size32_t RtlUtf8TypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  2534. {
  2535. size32_t sizeInChars;
  2536. rtlDataAttr value;
  2537. source.getUTF8Result(field, sizeInChars, value.refstr());
  2538. return buildUtf8(builder, offset, field, sizeInChars, value.getstr());
  2539. }
  2540. size32_t RtlUtf8TypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2541. {
  2542. size32_t sizeInBytes = rtlUtf8Size(sizeInChars, value);
  2543. assertex(!isFixedSize());
  2544. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), queryName(field));
  2545. byte *dest = builder.getSelf()+offset;
  2546. rtlWriteSize32t(dest, sizeInChars); // NOTE - in chars!
  2547. memcpy(dest+sizeof(size32_t), value, sizeInBytes);
  2548. return offset + sizeInBytes+sizeof(size32_t);
  2549. }
  2550. size32_t RtlUtf8TypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t sizeInChars, const char *value) const
  2551. {
  2552. assertex(!isFixedSize());
  2553. //MORE: The target size could be precalculated - which would avoid creating a temporary
  2554. size32_t tempLen;
  2555. rtlDataAttr temp;
  2556. rtlStrToUtf8X(tempLen, temp.refstr(), sizeInChars, value);
  2557. size32_t sizeInBytes = rtlUtf8Size(tempLen, temp.getstr());
  2558. builder.ensureCapacity(offset+sizeInBytes+sizeof(size32_t), queryName(field));
  2559. byte *dest = builder.getSelf()+offset;
  2560. rtlWriteSize32t(dest, sizeInChars); // NOTE - in chars!
  2561. memcpy((dest+sizeof(size32_t)), temp.getstr(), sizeInBytes);
  2562. return offset + sizeInBytes+sizeof(size32_t);
  2563. }
  2564. size32_t RtlUtf8TypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2565. {
  2566. assertex(!isFixedSize());
  2567. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  2568. unsigned thisLength = rtlReadSize32t(self);
  2569. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  2570. target.processUtf8(thisLength, str, field);
  2571. return thisSize;
  2572. }
  2573. size32_t RtlUtf8TypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  2574. {
  2575. assertex(!isFixedSize());
  2576. const char * str = reinterpret_cast<const char *>(self + sizeof(size32_t));
  2577. unsigned thisLength = rtlReadSize32t(self);
  2578. unsigned thisSize = sizeof(size32_t) + rtlUtf8Size(thisLength, str);
  2579. target.outputUtf8(thisLength, str, queryScalarXPath(field));
  2580. return thisSize;
  2581. }
  2582. size32_t RtlUtf8TypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  2583. {
  2584. assertex(!isFixedSize());
  2585. size32_t thisLength = in.readSize();
  2586. size32_t size = in.readUtf8(builder, offset + sizeof(size32_t), offset + sizeof(size32_t), thisLength);
  2587. rtlWriteSize32t(builder.getSelf() + offset, thisLength);
  2588. return offset + sizeof(size32_t) + size;
  2589. }
  2590. void RtlUtf8TypeInfo::readAhead(IRowPrefetcherSource & in) const
  2591. {
  2592. assertex(!isFixedSize());
  2593. size32_t thisLength = in.readSize();
  2594. in.skipUtf8(thisLength);
  2595. }
  2596. void RtlUtf8TypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2597. {
  2598. if (isFixedSize())
  2599. {
  2600. rtlUtf8ToStrX(resultLen, result, length, (const char *)ptr);
  2601. }
  2602. else
  2603. {
  2604. size32_t len = rtlReadSize32t(ptr);
  2605. rtlUtf8ToStrX(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  2606. }
  2607. }
  2608. void RtlUtf8TypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2609. {
  2610. if (isFixedSize())
  2611. {
  2612. rtlUtf8ToUtf8X(resultLen, result, length, (const char *)ptr);
  2613. }
  2614. else
  2615. {
  2616. size32_t len = rtlReadSize32t(ptr);
  2617. rtlUtf8ToUtf8X(resultLen, result, len, (const char *)ptr + sizeof(size32_t));
  2618. }
  2619. }
  2620. __int64 RtlUtf8TypeInfo::getInt(const void * ptr) const
  2621. {
  2622. //Utf8 output is the same as string output, so avoid the intermediate translation
  2623. if (isFixedSize())
  2624. return rtlUtf8ToInt(length, (const char *)ptr);
  2625. size32_t len = rtlReadSize32t(ptr);
  2626. return rtlUtf8ToInt(len, (const char *)ptr + sizeof(size32_t));
  2627. }
  2628. int RtlUtf8TypeInfo::compare(const byte * left, const byte * right) const
  2629. {
  2630. assertex(!isFixedSize());
  2631. size32_t lenLeft = rtlReadSize32t(left);
  2632. size32_t lenRight = rtlReadSize32t(right);
  2633. const char * valueLeft = reinterpret_cast<const char *>(left) + sizeof(size32_t);
  2634. const char * valueRight = reinterpret_cast<const char *>(right) + sizeof(size32_t);
  2635. return rtlCompareUtf8Utf8(lenLeft, valueLeft, lenRight, valueRight, locale);
  2636. }
  2637. int RtlUtf8TypeInfo::compareRange(size32_t lenLeft, const byte * left, size32_t lenRight, const byte * right) const
  2638. {
  2639. size32_t maxLeft = rtlReadSize32t(left);
  2640. size32_t maxRight = rtlReadSize32t(right);
  2641. lenLeft = std::min(lenLeft, maxLeft);
  2642. lenRight = std::min(lenRight, maxRight);
  2643. return rtlCompareUtf8Utf8(lenLeft, (const char *)(left + sizeof(size32_t)), lenRight, (const char *)(right + sizeof(size32_t)), locale);
  2644. }
  2645. void RtlUtf8TypeInfo::setBound(void * buffer, const byte * value, size32_t subLength, byte fill, bool inclusive) const
  2646. {
  2647. assertex(!isFixedSize());
  2648. byte *dst = (byte *) buffer;
  2649. size32_t minSize = getMinSize();
  2650. if (value)
  2651. {
  2652. assertex(inclusive);
  2653. if (subLength != MatchFullString)
  2654. {
  2655. //Note: Only variable length utf8 is supported by the system
  2656. size32_t copyLen = std::min(rtlReadSize32t(value), subLength);
  2657. size32_t copySize = rtlUtf8Size(copyLen, value + sizeof(size32_t));
  2658. *(size32_t *)buffer = copyLen;
  2659. memcpy(dst + sizeof(size32_t), value + sizeof(size32_t), copySize);
  2660. }
  2661. else
  2662. {
  2663. size32_t copySize = size(value, nullptr);
  2664. memcpy(dst, value, copySize);
  2665. }
  2666. }
  2667. else
  2668. {
  2669. //Generally cannot work for variable length fields - since always possible to create a larger value
  2670. //Default does not work for real types, or variable length signed integers
  2671. assertex(fill == 0);
  2672. memset(dst, fill, minSize);
  2673. }
  2674. }
  2675. unsigned RtlUtf8TypeInfo::hash(const byte * self, unsigned inhash) const
  2676. {
  2677. assertex(!isFixedSize());
  2678. size32_t len = rtlReadSize32t(self);
  2679. const char * uself = reinterpret_cast<const char *>(self + sizeof(size32_t));
  2680. return rtlHash32Utf8(rtlTrimUtf8StrLen(len, uself), uself,inhash);
  2681. }
  2682. //-------------------------------------------------------------------------------------------------------------------
  2683. inline size32_t sizeFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow)
  2684. {
  2685. unsigned offset = 0;
  2686. for (;;)
  2687. {
  2688. const RtlFieldInfo * child = *cur;
  2689. if (!child)
  2690. break;
  2691. offset += child->size(self+offset, selfrow);
  2692. cur++;
  2693. }
  2694. return offset;
  2695. }
  2696. static size32_t processFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IFieldProcessor & target)
  2697. {
  2698. unsigned offset = 0;
  2699. for (;;)
  2700. {
  2701. const RtlFieldInfo * child = *cur;
  2702. if (!child)
  2703. break;
  2704. child->process(self+offset, selfrow, target);
  2705. offset += child->size(self+offset, selfrow);
  2706. cur++;
  2707. }
  2708. return offset;
  2709. }
  2710. static size32_t buildFields(const RtlFieldInfo * const * cur, ARowBuilder &builder, size32_t offset, IFieldSource &source)
  2711. {
  2712. for (;;)
  2713. {
  2714. const RtlFieldInfo * child = *cur;
  2715. if (!child)
  2716. break;
  2717. offset = child->build(builder, offset, source);
  2718. cur++;
  2719. }
  2720. return offset;
  2721. }
  2722. static size32_t toXMLFields(const RtlFieldInfo * const * cur, const byte * self, const byte * selfrow, IXmlWriter & target)
  2723. {
  2724. size32_t offset = 0;
  2725. for (;;)
  2726. {
  2727. const RtlFieldInfo * child = *cur;
  2728. if (!child)
  2729. break;
  2730. size32_t size = child->toXML(self+offset, selfrow, target);
  2731. offset += size;
  2732. cur++;
  2733. }
  2734. return offset;
  2735. }
  2736. static size32_t deserializeFields(const RtlFieldInfo * const * cur, ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset)
  2737. {
  2738. for (;;)
  2739. {
  2740. const RtlFieldInfo * child = *cur;
  2741. if (!child)
  2742. break;
  2743. offset = child->type->deserialize(builder, in, offset);
  2744. cur++;
  2745. }
  2746. return offset;
  2747. }
  2748. static void readAheadFields(const RtlFieldInfo * const * cur, IRowPrefetcherSource & in)
  2749. {
  2750. for (;;)
  2751. {
  2752. const RtlFieldInfo * child = *cur;
  2753. if (!child)
  2754. break;
  2755. child->type->readAhead(in);
  2756. cur++;
  2757. }
  2758. }
  2759. int ECLRTL_API compareFields(const RtlFieldInfo * const * cur, const byte * left, const byte * right, bool excludePayload)
  2760. {
  2761. size32_t leftOffset = 0;
  2762. size32_t rightOffset = 0;
  2763. for (;;)
  2764. {
  2765. const RtlFieldInfo * child = *cur;
  2766. if (!child || (excludePayload && (child->flags & RFTMispayloadfield)))
  2767. return 0;
  2768. auto type = child->type;
  2769. int rc = type->compare(left + leftOffset, right + rightOffset);
  2770. if (rc != 0)
  2771. return rc;
  2772. leftOffset += type->size(left + leftOffset, left);
  2773. rightOffset += type->size(right + rightOffset, right);
  2774. cur++;
  2775. }
  2776. }
  2777. unsigned ECLRTL_API hashFields(const RtlFieldInfo * const * cur, const byte *self, unsigned inhash, bool excludePayload)
  2778. {
  2779. size32_t offset = 0;
  2780. for (;;)
  2781. {
  2782. const RtlFieldInfo * child = *cur;
  2783. if (!child || (excludePayload && (child->flags & RFTMispayloadfield)))
  2784. break;
  2785. auto type = child->type;
  2786. inhash = type->hash(self + offset, inhash);
  2787. offset += type->size(self + offset, self);
  2788. cur++;
  2789. }
  2790. return inhash;
  2791. }
  2792. extern bool ECLRTL_API hasTrailingFileposition(const RtlFieldInfo * const * fields)
  2793. {
  2794. if (!*fields)
  2795. return false;
  2796. while (*fields)
  2797. fields++;
  2798. return (fields[-1]->type->getType() == type_filepos); // note - filepos is created as an RtlSwapIntTypeInfo with type set to type_filepos
  2799. }
  2800. extern bool ECLRTL_API hasTrailingFileposition(const RtlTypeInfo * type)
  2801. {
  2802. switch (type->getType())
  2803. {
  2804. case type_record:
  2805. {
  2806. const RtlRecordTypeInfo * record = static_cast<const RtlRecordTypeInfo *>(type);
  2807. return hasTrailingFileposition(record->fields);
  2808. }
  2809. }
  2810. return false;
  2811. }
  2812. //-------------------------------------------------------------------------------------------------------------------
  2813. size32_t RtlRecordTypeInfo::getMinSize() const
  2814. {
  2815. return ::getMinSize(fields);
  2816. }
  2817. size32_t RtlRecordTypeInfo::size(const byte * self, const byte * selfrow) const
  2818. {
  2819. return sizeFields(fields, self, self);
  2820. }
  2821. size32_t RtlRecordTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2822. {
  2823. if (target.processBeginRow(field))
  2824. {
  2825. unsigned offset = processFields(fields, self, self, target);
  2826. target.processEndRow(field);
  2827. return offset;
  2828. }
  2829. return size(self, selfrow);
  2830. }
  2831. size32_t RtlRecordTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  2832. {
  2833. const char * xpath = queryXPath(field);
  2834. if (*xpath)
  2835. target.outputBeginNested(xpath, false);
  2836. unsigned thisSize = toXMLFields(fields, self, self, target);
  2837. if (*xpath)
  2838. target.outputEndNested(xpath);
  2839. return thisSize;
  2840. }
  2841. size32_t RtlRecordTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  2842. {
  2843. //Will not generally be called because it will have been expanded
  2844. return deserializeFields(fields, builder, in, offset);
  2845. }
  2846. void RtlRecordTypeInfo::readAhead(IRowPrefetcherSource & in) const
  2847. {
  2848. //This could be called if the record contains ifblocks
  2849. in.noteStartChild();
  2850. readAheadFields(fields, in);
  2851. in.noteFinishChild();
  2852. }
  2853. size32_t RtlRecordTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  2854. {
  2855. source.processBeginRow(field);
  2856. offset = buildFields(fields, builder, offset, source);
  2857. source.processEndRow(field);
  2858. return offset;
  2859. }
  2860. size32_t RtlRecordTypeInfo::buildNull(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field) const
  2861. {
  2862. const RtlFieldInfo * const * cur = fields;
  2863. for (;;)
  2864. {
  2865. const RtlFieldInfo * child = *cur;
  2866. if (!child)
  2867. break;
  2868. offset = child->type->buildNull(builder, offset, child);
  2869. cur++;
  2870. }
  2871. return offset;
  2872. }
  2873. size32_t RtlRecordTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  2874. {
  2875. throwUnexpected();
  2876. }
  2877. size32_t RtlRecordTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  2878. {
  2879. throwUnexpected();
  2880. }
  2881. void RtlRecordTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2882. {
  2883. resultLen = 0;
  2884. result = nullptr;
  2885. }
  2886. void RtlRecordTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2887. {
  2888. resultLen = 0;
  2889. result = nullptr;
  2890. }
  2891. __int64 RtlRecordTypeInfo::getInt(const void * ptr) const
  2892. {
  2893. return 0;
  2894. }
  2895. int RtlRecordTypeInfo::compare(const byte * left, const byte * right) const
  2896. {
  2897. return compareFields(fields, left, right);
  2898. }
  2899. unsigned RtlRecordTypeInfo::hash(const byte * self, unsigned inhash) const
  2900. {
  2901. return hashFields(fields, self, inhash);
  2902. }
  2903. //-------------------------------------------------------------------------------------------------------------------
  2904. size32_t RtlCompoundTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  2905. {
  2906. UNIMPLEMENTED;
  2907. }
  2908. size32_t RtlCompoundTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  2909. {
  2910. UNIMPLEMENTED;
  2911. }
  2912. void RtlCompoundTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  2913. {
  2914. //MORE: Should this fail instead?
  2915. resultLen = 0;
  2916. result = nullptr;
  2917. }
  2918. void RtlCompoundTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  2919. {
  2920. //MORE: Should this fail instead?
  2921. resultLen = 0;
  2922. result = nullptr;
  2923. }
  2924. __int64 RtlCompoundTypeInfo::getInt(const void * ptr) const
  2925. {
  2926. //MORE: Should this fail instead?
  2927. return 0;
  2928. }
  2929. //-------------------------------------------------------------------------------------------------------------------
  2930. size32_t RtlSetTypeInfo::getMinSize() const
  2931. {
  2932. return sizeof(bool) + sizeof(size32_t);
  2933. }
  2934. size32_t RtlSetTypeInfo::size(const byte * self, const byte * selfrow) const
  2935. {
  2936. return sizeof(bool) + sizeof(size32_t) + rtlReadSize32t(self + sizeof(bool));
  2937. }
  2938. size32_t RtlSetTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  2939. {
  2940. bool isAll;
  2941. source.processBeginSet(field, isAll);
  2942. size32_t sizeInBytes = sizeof(bool) + sizeof(size32_t);
  2943. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  2944. byte *dest = builder.getSelf()+offset;
  2945. if (isAll)
  2946. {
  2947. * (bool *) dest = true;
  2948. rtlWriteInt4(dest+1, 0);
  2949. offset += sizeInBytes;
  2950. }
  2951. else
  2952. {
  2953. * (bool *) dest = false;
  2954. size32_t newOffset = offset + sizeInBytes;
  2955. RtlFieldStrInfo dummyField("<set element>", NULL, child);
  2956. while (source.processNextSet(field))
  2957. {
  2958. newOffset = child->build(builder, newOffset, &dummyField, source);
  2959. }
  2960. // Go back in and patch the size, remembering it may have moved
  2961. rtlWriteInt4(builder.getSelf()+offset+1, newOffset - (offset+sizeInBytes));
  2962. offset = newOffset;
  2963. }
  2964. source.processEndSet(field);
  2965. return offset;
  2966. }
  2967. size32_t RtlSetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  2968. {
  2969. unsigned offset = sizeof(bool) + sizeof(size32_t);
  2970. unsigned max = offset + rtlReadSize32t(self + sizeof(bool));
  2971. unsigned elements = 0;
  2972. if (!*(bool *)self)
  2973. {
  2974. unsigned tempOffset = sizeof(bool) + sizeof(size32_t);
  2975. if (child->isFixedSize())
  2976. {
  2977. unsigned elemSize = child->size(NULL, NULL);
  2978. elements = (max-offset) / elemSize;
  2979. assert(elements*elemSize == max-offset);
  2980. }
  2981. else
  2982. {
  2983. DummyFieldProcessor dummy;
  2984. while (tempOffset < max)
  2985. {
  2986. tempOffset += child->process(self+tempOffset, selfrow, field, dummy); // NOTE - good thing we can't have a set of sets, or this would recurse
  2987. elements++;
  2988. }
  2989. }
  2990. }
  2991. if (target.processBeginSet(field, elements, *(bool *)self, self+offset))
  2992. {
  2993. while (offset < max)
  2994. {
  2995. offset += child->process(self+offset, selfrow, field, target);
  2996. }
  2997. }
  2998. target.processEndSet(field);
  2999. return max;
  3000. }
  3001. size32_t RtlSetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3002. {
  3003. unsigned offset = sizeof(bool) + sizeof(size32_t);
  3004. unsigned max = offset + rtlReadSize32t(self + sizeof(bool));
  3005. StringAttr outerTag;
  3006. if (hasOuterXPath(field))
  3007. {
  3008. queryNestedOuterXPath(outerTag, field);
  3009. target.outputBeginNested(outerTag, false);
  3010. }
  3011. if (*(bool *)self)
  3012. target.outputSetAll();
  3013. else
  3014. {
  3015. const char *innerPath = queryXPath(field);
  3016. target.outputBeginArray(innerPath);
  3017. while (offset < max)
  3018. {
  3019. child->toXML(self+offset, selfrow, field, target);
  3020. offset += child->size(self+offset, selfrow);
  3021. }
  3022. target.outputEndArray(innerPath);
  3023. }
  3024. if (outerTag)
  3025. target.outputEndNested(outerTag);
  3026. return max;
  3027. }
  3028. size32_t RtlSetTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3029. {
  3030. bool isAll;
  3031. in.read(1, &isAll);
  3032. size32_t size = in.readSize();
  3033. byte * dest = builder.ensureCapacity(offset+sizeof(bool)+sizeof(size32_t)+size, nullptr) + offset;
  3034. *dest = isAll;
  3035. rtlWriteSize32t(dest + sizeof(bool), size);
  3036. in.read(size, dest + sizeof(bool) + sizeof(size32_t));
  3037. return offset+sizeof(bool)+sizeof(size32_t)+size;
  3038. }
  3039. void RtlSetTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3040. {
  3041. in.skip(1);
  3042. size32_t thisLength = in.readSize();
  3043. in.skip(thisLength);
  3044. }
  3045. int RtlSetTypeInfo::compare(const byte * left, const byte * right) const
  3046. {
  3047. const bool allLeft = *(const bool *)left;
  3048. const bool allRight = *(const bool *)right;
  3049. if (allLeft || allRight)
  3050. {
  3051. if (allLeft && allRight)
  3052. return 0;
  3053. if (allLeft)
  3054. return +1;
  3055. else
  3056. return -1;
  3057. }
  3058. size32_t sizeLeft = rtlReadSize32t(left + sizeof(bool));
  3059. const byte * ptrLeft = left + sizeof(bool) + sizeof(size32_t);
  3060. size32_t sizeRight = rtlReadSize32t(right + sizeof(bool));
  3061. const byte * ptrRight = right + sizeof(bool) + sizeof(size32_t);
  3062. size32_t offLeft = 0;
  3063. size32_t offRight = 0;
  3064. for (;;)
  3065. {
  3066. if ((offLeft == sizeLeft) || (offRight == sizeRight))
  3067. {
  3068. if ((offLeft == sizeLeft) && (offRight == sizeRight))
  3069. return 0;
  3070. if (offLeft == sizeLeft)
  3071. return -1;
  3072. else
  3073. return +1;
  3074. }
  3075. int rc = child->compare(ptrLeft + offLeft, ptrRight + offRight);
  3076. if (rc != 0)
  3077. return rc;
  3078. offLeft += child->size(ptrLeft + offLeft, ptrLeft + offLeft);
  3079. offRight += child->size(ptrRight + offRight, ptrRight + offRight);
  3080. }
  3081. }
  3082. unsigned RtlSetTypeInfo::hash(const byte * self, unsigned inhash) const
  3083. {
  3084. const bool allLeft = *(const bool *) self;
  3085. self += sizeof(bool);
  3086. if (allLeft)
  3087. {
  3088. // Nothing - this is unfortunate as it means hash(all) and hash([]) are the same...
  3089. // But it matches generated code
  3090. }
  3091. else
  3092. {
  3093. size32_t size = rtlReadSize32t(self);
  3094. self += sizeof(size32_t);
  3095. #if 0
  3096. // This might be the smart way to hash - since it means that the hash depends on the value, not the type,
  3097. // and that things that compare equal will hash equal. But that is not what the code generator does
  3098. for (size32_t offset = 0; offset < size; offset += child->size(self + offset, self + offset))
  3099. {
  3100. inhash = child->hash(self + offset, inhash);
  3101. }
  3102. #else
  3103. inhash = rtlHash32Data(size, self, inhash);
  3104. #endif
  3105. }
  3106. return inhash;
  3107. }
  3108. //-------------------------------------------------------------------------------------------------------------------
  3109. size32_t RtlRowTypeInfo::getMinSize() const
  3110. {
  3111. if (isLinkCounted())
  3112. return sizeof(void *);
  3113. return child->getMinSize();
  3114. }
  3115. size32_t RtlRowTypeInfo::size(const byte * self, const byte * selfrow) const
  3116. {
  3117. if (isLinkCounted())
  3118. return sizeof(void *);
  3119. return child->size(self, selfrow);
  3120. }
  3121. size32_t RtlRowTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3122. {
  3123. if (isLinkCounted())
  3124. {
  3125. const byte * row = *(const byte * *)self;
  3126. if (row)
  3127. child->process(row, row, field, target);
  3128. return sizeof(row);
  3129. }
  3130. return child->process(self, self, field, target);
  3131. }
  3132. size32_t RtlRowTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3133. {
  3134. if (isLinkCounted())
  3135. {
  3136. const byte * row = *(const byte * *)self;
  3137. child->toXML(row, row, field, target);
  3138. return sizeof(row);
  3139. }
  3140. return child->toXML(self, self, field, target);
  3141. }
  3142. size32_t RtlRowTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3143. {
  3144. //Link counted isn't yet implemented in the code generator
  3145. if (isLinkCounted())
  3146. UNIMPLEMENTED;
  3147. return child->deserialize(builder, in, offset);
  3148. }
  3149. void RtlRowTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3150. {
  3151. return child->readAhead(in);
  3152. }
  3153. int RtlRowTypeInfo::compare(const byte * left, const byte * right) const
  3154. {
  3155. if (isLinkCounted())
  3156. {
  3157. const byte * leftRow = *(const byte * *)left;
  3158. const byte * rightRow = *(const byte * *)right;
  3159. if (leftRow && rightRow)
  3160. return child->compare(leftRow, rightRow);
  3161. if (leftRow)
  3162. return +1;
  3163. if (rightRow)
  3164. return -1;
  3165. return 0;
  3166. }
  3167. return child->compare(left, right);
  3168. }
  3169. unsigned RtlRowTypeInfo::hash(const byte * self, unsigned inhash) const
  3170. {
  3171. if (isLinkCounted())
  3172. {
  3173. const byte * selfRow = *(const byte * *)self;
  3174. if (selfRow)
  3175. inhash = child->hash(selfRow, inhash);
  3176. return inhash;
  3177. }
  3178. return child->hash(self, inhash);
  3179. }
  3180. //-------------------------------------------------------------------------------------------------------------------
  3181. size32_t RtlDatasetTypeInfo::getMinSize() const
  3182. {
  3183. if (isLinkCounted())
  3184. return sizeof(size32_t) + sizeof(void * *);
  3185. return sizeof(size32_t);
  3186. }
  3187. size32_t RtlDatasetTypeInfo::size(const byte * self, const byte * selfrow) const
  3188. {
  3189. if (isLinkCounted())
  3190. return sizeof(size32_t) + sizeof(void * *);
  3191. return sizeof(size32_t) + rtlReadSize32t(self);
  3192. }
  3193. size32_t RtlDatasetTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  3194. {
  3195. source.processBeginDataset(field);
  3196. if (isLinkCounted())
  3197. {
  3198. // a 32-bit record count, and a pointer to an array of record pointers
  3199. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  3200. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  3201. size32_t numRows = 0;
  3202. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  3203. const byte **childRows = NULL;
  3204. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  3205. while (source.processNextRow(field))
  3206. {
  3207. RtlDynamicRowBuilder childBuilder(*childAllocator);
  3208. size32_t childLen = child->build(childBuilder, 0, &dummyField, source);
  3209. childRows = childAllocator->appendRowOwn(childRows, ++numRows, (void *) childBuilder.finalizeRowClear(childLen));
  3210. }
  3211. // Go back in and patch the count, remembering it may have moved
  3212. rtlWriteInt4(builder.getSelf()+offset, numRows);
  3213. * ( const void * * ) (builder.getSelf()+offset+sizeof(size32_t)) = childRows;
  3214. offset += sizeInBytes;
  3215. }
  3216. else
  3217. {
  3218. // a 32-bit size, then rows inline
  3219. size32_t sizeInBytes = sizeof(size32_t);
  3220. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  3221. size32_t newOffset = offset + sizeInBytes;
  3222. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  3223. while (source.processNextRow(field))
  3224. newOffset = child->build(builder, newOffset, &dummyField, source);
  3225. // Go back in and patch the size, remembering it may have moved
  3226. rtlWriteInt4(builder.getSelf()+offset, newOffset - (offset+sizeInBytes));
  3227. offset = newOffset;
  3228. }
  3229. source.processEndDataset(field);
  3230. return offset;
  3231. }
  3232. size32_t RtlDatasetTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3233. {
  3234. if (isLinkCounted())
  3235. {
  3236. size32_t thisCount = rtlReadSize32t(self);
  3237. if (target.processBeginDataset(field, thisCount))
  3238. {
  3239. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  3240. for (unsigned i= 0; i < thisCount; i++)
  3241. {
  3242. const byte * row = rows[i];
  3243. child->process(row, row, field, target);
  3244. }
  3245. target.processEndDataset(field);
  3246. }
  3247. return sizeof(size32_t) + sizeof(void * *);
  3248. }
  3249. else
  3250. {
  3251. unsigned offset = sizeof(size32_t);
  3252. unsigned max = offset + rtlReadSize32t(self);
  3253. unsigned thisCount = 0;
  3254. DummyFieldProcessor dummy;
  3255. while (offset < max)
  3256. {
  3257. offset += child->process(self+offset, self+offset, field, dummy);
  3258. thisCount++;
  3259. }
  3260. offset = sizeof(size32_t);
  3261. if (target.processBeginDataset(field, thisCount))
  3262. {
  3263. while (offset < max)
  3264. {
  3265. offset += child->process(self+offset, self+offset, field, target);
  3266. }
  3267. target.processEndDataset(field);
  3268. }
  3269. return max;
  3270. }
  3271. }
  3272. size32_t RtlDatasetTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3273. {
  3274. StringAttr outerTag;
  3275. if (hasOuterXPath(field))
  3276. {
  3277. queryNestedOuterXPath(outerTag, field);
  3278. target.outputBeginNested(outerTag, false);
  3279. }
  3280. const char *innerPath = queryXPath(field);
  3281. target.outputBeginArray(innerPath);
  3282. unsigned thisSize;
  3283. if (isLinkCounted())
  3284. {
  3285. size32_t thisCount = rtlReadSize32t(self);
  3286. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  3287. for (unsigned i= 0; i < thisCount; i++)
  3288. {
  3289. const byte * row = rows[i];
  3290. if (row)
  3291. child->toXML(row, row, field, target);
  3292. }
  3293. thisSize = sizeof(size32_t) + sizeof(void * *);
  3294. }
  3295. else
  3296. {
  3297. unsigned offset = sizeof(size32_t);
  3298. unsigned max = offset + rtlReadSize32t(self);
  3299. while (offset < max)
  3300. {
  3301. child->toXML(self+offset, self+offset, field, target);
  3302. offset += child->size(self+offset, self+offset);
  3303. }
  3304. thisSize = max;
  3305. }
  3306. target.outputEndArray(innerPath);
  3307. if (outerTag)
  3308. target.outputEndNested(outerTag);
  3309. return thisSize;
  3310. }
  3311. size32_t RtlDatasetTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3312. {
  3313. const bool canUseMemcpy = true;
  3314. if (canUseMemcpy && !isLinkCounted())
  3315. {
  3316. size32_t size = in.readSize();
  3317. byte * dest = builder.ensureCapacity(offset+sizeof(size32_t)+size, nullptr) + offset;
  3318. rtlWriteSize32t(dest, size);
  3319. in.read(size, dest + sizeof(size32_t));
  3320. return offset + sizeof(size32_t) + size;
  3321. }
  3322. else
  3323. {
  3324. if (isLinkCounted())
  3325. {
  3326. //Currently inefficient because it is recreating deserializers and resolving child allocators each time it is called.
  3327. ICodeContext * ctx = nullptr; // Slightly dodgy, but not needed if the child deserializers are also calculated
  3328. unsigned activityId = 0;
  3329. // a 32-bit record count, and a pointer to an hash table with record pointers
  3330. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  3331. byte * dest = builder.ensureCapacity(offset+sizeInBytes, nullptr) + offset;
  3332. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  3333. Owned<IOutputRowDeserializer> deserializer = childAllocator->queryOutputMeta()->createDiskDeserializer(ctx, activityId);
  3334. rtlDeserializeChildRowset(*(size32_t *)dest, *(const byte * * *)(dest + sizeof(size32_t)), childAllocator, deserializer, in);
  3335. return offset + sizeInBytes;
  3336. }
  3337. else
  3338. {
  3339. size32_t startOffset = offset + sizeof(size32_t);
  3340. size32_t nextOffset = startOffset;
  3341. offset_t endOffset = in.beginNested();
  3342. while (in.finishedNested(endOffset))
  3343. nextOffset = child->deserialize(builder, in, nextOffset);
  3344. rtlWriteSize32t(builder.getSelf() + offset, nextOffset - startOffset);
  3345. return nextOffset;
  3346. }
  3347. }
  3348. }
  3349. void RtlDatasetTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3350. {
  3351. size32_t size = in.readSize();
  3352. in.skip(size);
  3353. }
  3354. int RtlDatasetTypeInfo::compare(const byte * left, const byte * right) const
  3355. {
  3356. if (isLinkCounted())
  3357. {
  3358. const size32_t countLeft = rtlReadSize32t(left);
  3359. const size32_t countRight = rtlReadSize32t(right);
  3360. const byte * * rowsLeft = (const byte * *)((const byte *)left + sizeof(size32_t));
  3361. const byte * * rowsRight = (const byte * *)((const byte *)right + sizeof(size32_t));
  3362. size32_t row = 0;
  3363. for (;;)
  3364. {
  3365. if ((row == countLeft) || (row == countRight))
  3366. {
  3367. if (countLeft == countRight)
  3368. return 0;
  3369. if (row == countLeft)
  3370. return -1;
  3371. else
  3372. return +1;
  3373. }
  3374. int rc = child->compare(rowsLeft[row], rowsRight[row]);
  3375. if (rc != 0)
  3376. return rc;
  3377. row++;
  3378. }
  3379. }
  3380. size32_t lenLeft = rtlReadSize32t(left);
  3381. size32_t lenRight = rtlReadSize32t(right);
  3382. const byte * ptrLeft = left + sizeof(size32_t);
  3383. const byte * ptrRight = right + sizeof(size32_t);
  3384. size32_t offLeft = 0;
  3385. size32_t offRight = 0;
  3386. for (;;)
  3387. {
  3388. if ((offLeft == lenLeft) || (offRight == lenRight))
  3389. {
  3390. if ((offLeft == lenLeft) && (offRight == lenRight))
  3391. return 0;
  3392. if (offLeft == lenLeft)
  3393. return -1;
  3394. else
  3395. return +1;
  3396. }
  3397. int rc = child->compare(ptrLeft + offLeft, ptrRight + offRight);
  3398. if (rc != 0)
  3399. return rc;
  3400. offLeft += child->size(ptrLeft + offLeft, ptrLeft + offLeft);
  3401. offRight += child->size(ptrRight + offRight, ptrRight + offRight);
  3402. }
  3403. }
  3404. unsigned RtlDatasetTypeInfo::hash(const byte * self, unsigned inhash) const
  3405. {
  3406. if (isLinkCounted())
  3407. {
  3408. const size32_t count = rtlReadSize32t(self);
  3409. self += sizeof(size32_t);
  3410. const byte * * rows = (const byte * *) self;
  3411. for (size32_t row = 0; row < count; row++)
  3412. {
  3413. inhash = child->hash(rows[row], inhash);
  3414. }
  3415. }
  3416. else
  3417. {
  3418. size32_t len = rtlReadSize32t(self);
  3419. self += sizeof(size32_t);
  3420. for (size32_t offset = 0; offset < len; offset += child->size(self + offset, self + offset))
  3421. {
  3422. inhash = child->hash(self + offset, inhash);
  3423. }
  3424. }
  3425. return inhash;
  3426. }
  3427. //-------------------------------------------------------------------------------------------------------------------
  3428. size32_t RtlDictionaryTypeInfo::getMinSize() const
  3429. {
  3430. if (isLinkCounted())
  3431. return sizeof(size32_t) + sizeof(void * *);
  3432. return sizeof(size32_t);
  3433. }
  3434. size32_t RtlDictionaryTypeInfo::size(const byte * self, const byte * selfrow) const
  3435. {
  3436. if (isLinkCounted())
  3437. return sizeof(size32_t) + sizeof(void * *);
  3438. return sizeof(size32_t) + rtlReadSize32t(self);
  3439. }
  3440. size32_t RtlDictionaryTypeInfo::build(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, IFieldSource &source) const
  3441. {
  3442. source.processBeginDataset(field);
  3443. if (isLinkCounted())
  3444. {
  3445. // a 32-bit record count, and a pointer to an hash table with record pointers
  3446. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  3447. builder.ensureCapacity(offset+sizeInBytes, queryName(field));
  3448. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  3449. CHThorHashLookupInfo hashInfo(*static_cast<const RtlRecordTypeInfo *>(child));
  3450. RtlLinkedDictionaryBuilder dictBuilder(childAllocator, &hashInfo);
  3451. RtlFieldStrInfo dummyField("<nested row>", NULL, child);
  3452. while (source.processNextRow(field))
  3453. {
  3454. RtlDynamicRowBuilder childBuilder(childAllocator);
  3455. size32_t childLen = child->build(childBuilder, 0, &dummyField, source);
  3456. dictBuilder.appendOwn((void *) childBuilder.finalizeRowClear(childLen));
  3457. }
  3458. // Go back in and patch the count
  3459. rtlWriteInt4(builder.getSelf()+offset, dictBuilder.getcount());
  3460. * ( const void * * ) (builder.getSelf()+offset+sizeof(size32_t)) = dictBuilder.linkrows();
  3461. offset += sizeInBytes;
  3462. }
  3463. else
  3464. throwUnexpected(); // And may never be...
  3465. source.processEndDataset(field);
  3466. return offset;
  3467. }
  3468. size32_t RtlDictionaryTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3469. {
  3470. if (isLinkCounted())
  3471. {
  3472. size32_t thisCount = rtlReadSize32t(self);
  3473. if (target.processBeginDataset(field, thisCount))
  3474. {
  3475. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  3476. for (unsigned i= 0; i < thisCount; i++)
  3477. {
  3478. const byte * row = rows[i];
  3479. if (row)
  3480. child->process(row, row, field, target);
  3481. }
  3482. target.processEndDataset(field);
  3483. }
  3484. return sizeof(size32_t) + sizeof(void * *);
  3485. }
  3486. else
  3487. {
  3488. //MORE: We could interpret serialized dictionaries if there was ever a need
  3489. throwUnexpected();
  3490. }
  3491. }
  3492. size32_t RtlDictionaryTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3493. {
  3494. StringAttr outerTag;
  3495. if (hasOuterXPath(field))
  3496. {
  3497. queryNestedOuterXPath(outerTag, field);
  3498. target.outputBeginNested(outerTag, false);
  3499. }
  3500. const char *innerPath = queryXPath(field);
  3501. target.outputBeginArray(innerPath);
  3502. unsigned thisSize;
  3503. if (isLinkCounted())
  3504. {
  3505. size32_t thisCount = rtlReadSize32t(self);
  3506. const byte * * rows = *reinterpret_cast<const byte * * const *>(self + sizeof(size32_t));
  3507. for (unsigned i= 0; i < thisCount; i++)
  3508. {
  3509. const byte * row = rows[i];
  3510. if (row)
  3511. child->toXML(row, row, field, target);
  3512. }
  3513. thisSize = sizeof(size32_t) + sizeof(void * *);
  3514. }
  3515. else
  3516. {
  3517. //MORE: We could interpret serialized dictionaries if there was ever a need
  3518. throwUnexpected();
  3519. }
  3520. target.outputEndArray(innerPath);
  3521. if (outerTag)
  3522. target.outputEndNested(outerTag);
  3523. return thisSize;
  3524. }
  3525. size32_t RtlDictionaryTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3526. {
  3527. if (isLinkCounted())
  3528. {
  3529. //Currently inefficient because it is recreating deserializers and resolving child allocators each time it is called.
  3530. ICodeContext * ctx = nullptr; // Slightly dodgy, but not needed if the child deserializers are also calculated
  3531. unsigned activityId = 0;
  3532. // a 32-bit record count, and a pointer to an hash table with record pointers
  3533. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  3534. byte * dest = builder.ensureCapacity(offset + sizeInBytes, nullptr) + offset;
  3535. Owned<IEngineRowAllocator> childAllocator = builder.queryAllocator()->createChildRowAllocator(child);
  3536. Owned<IOutputRowDeserializer> deserializer = childAllocator->queryOutputMeta()->createDiskDeserializer(ctx, activityId);
  3537. rtlDeserializeChildDictionary(*(size32_t *)dest, *(const byte * * *)(dest + sizeof(size32_t)), childAllocator, deserializer, in);
  3538. return offset + sizeInBytes;
  3539. }
  3540. else
  3541. {
  3542. UNIMPLEMENTED;
  3543. }
  3544. }
  3545. void RtlDictionaryTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3546. {
  3547. size32_t size = in.readSize();
  3548. in.skip(size);
  3549. }
  3550. int RtlDictionaryTypeInfo::compare(const byte * left, const byte * right) const
  3551. {
  3552. if (isLinkCounted())
  3553. {
  3554. const size32_t countLeft = rtlReadSize32t(left);
  3555. const size32_t countRight = rtlReadSize32t(right);
  3556. const byte * * rowsLeft = (const byte * *)((const byte *)left + sizeof(size32_t));
  3557. const byte * * rowsRight = (const byte * *)((const byte *)right + sizeof(size32_t));
  3558. size32_t leftRow = 0;
  3559. size32_t rightRow = 0;
  3560. for (;;)
  3561. {
  3562. //Dictionaries are compared as datasets => skip until the first non-null entry
  3563. while ((leftRow != countLeft) && !rowsLeft[leftRow])
  3564. leftRow++;
  3565. while ((rightRow != countRight) && !rowsRight[rightRow])
  3566. rightRow++;
  3567. if ((leftRow == countLeft) || (rightRow == countRight))
  3568. {
  3569. if ((leftRow == countLeft) && (rightRow == countRight))
  3570. return 0;
  3571. if (leftRow == countLeft)
  3572. return -1;
  3573. else
  3574. return +1;
  3575. }
  3576. int rc = child->compare(rowsLeft[leftRow], rowsRight[rightRow]);
  3577. if (rc != 0)
  3578. return rc;
  3579. leftRow++;
  3580. rightRow++;
  3581. }
  3582. }
  3583. else
  3584. {
  3585. //Non LCR dictionaries are not supported.
  3586. throwUnexpected();
  3587. }
  3588. }
  3589. unsigned RtlDictionaryTypeInfo::hash(const byte * self, unsigned inhash) const
  3590. {
  3591. if (isLinkCounted())
  3592. {
  3593. const size32_t count = rtlReadSize32t(self);
  3594. self += sizeof(size32_t);
  3595. const byte * * rows = (const byte * *) self;
  3596. size32_t row = 0;
  3597. for (;;)
  3598. {
  3599. //Dictionaries are compared as datasets => skip until the first non-null entry
  3600. while ((row != count) && !rows[row])
  3601. row++;
  3602. if (row == count)
  3603. break;
  3604. inhash = child->hash(rows[row], inhash);
  3605. row++;
  3606. }
  3607. }
  3608. else
  3609. {
  3610. //Non LCR dictionaries are not supported.
  3611. throwUnexpected();
  3612. }
  3613. return inhash;
  3614. }
  3615. //-------------------------------------------------------------------------------------------------------------------
  3616. size32_t RtlIfBlockTypeInfo::getMinSize() const
  3617. {
  3618. return 0;
  3619. }
  3620. size32_t RtlIfBlockTypeInfo::size(const byte * self, const byte * selfrow) const
  3621. {
  3622. if (getCondition(selfrow))
  3623. return sizeFields(fields, self, selfrow);
  3624. return 0;
  3625. }
  3626. size32_t RtlIfBlockTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  3627. {
  3628. throwUnexpected();
  3629. }
  3630. size32_t RtlIfBlockTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  3631. {
  3632. throwUnexpected();
  3633. }
  3634. size32_t RtlIfBlockTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3635. {
  3636. if (getCondition(selfrow))
  3637. return processFields(fields, self, selfrow, target);
  3638. return 0;
  3639. }
  3640. size32_t RtlIfBlockTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3641. {
  3642. if (getCondition(selfrow))
  3643. return toXMLFields(fields, self, selfrow, target);
  3644. return 0;
  3645. }
  3646. size32_t RtlIfBlockTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3647. {
  3648. if (getCondition(builder.getSelf()))
  3649. return deserializeFields(fields, builder, in, offset);
  3650. return offset;
  3651. }
  3652. void RtlIfBlockTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3653. {
  3654. if (getCondition(in.querySelf()))
  3655. readAheadFields(fields, in);
  3656. }
  3657. void RtlIfBlockTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  3658. {
  3659. //MORE: Should this fail instead?
  3660. resultLen = 0;
  3661. result = nullptr;
  3662. }
  3663. void RtlIfBlockTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  3664. {
  3665. //MORE: Should this fail instead?
  3666. resultLen = 0;
  3667. result = nullptr;
  3668. }
  3669. __int64 RtlIfBlockTypeInfo::getInt(const void * ptr) const
  3670. {
  3671. //MORE: Should this fail instead?
  3672. return 0;
  3673. }
  3674. int RtlIfBlockTypeInfo::compare(const byte * left, const byte * right) const
  3675. {
  3676. bool includeLeft = getCondition(left);
  3677. bool includeRight = getCondition(right);
  3678. if (includeLeft && includeRight)
  3679. return compareFields(fields, left, right);
  3680. //If the fields are all blank then this isn't actually correct, but that is unlikely to be a problem
  3681. if (includeLeft)
  3682. return +1;
  3683. else if (includeRight)
  3684. return -1;
  3685. else
  3686. return 0;
  3687. }
  3688. unsigned RtlIfBlockTypeInfo::hash(const byte * self, unsigned inhash) const
  3689. {
  3690. bool included = getCondition(self);
  3691. if (included)
  3692. inhash = hashFields(fields, self, inhash);
  3693. return inhash;
  3694. }
  3695. bool RtlComplexIfBlockTypeInfo::getCondition(const RtlRow & selfrow) const
  3696. {
  3697. //Call the function in the derived class that evaluates the test condition
  3698. return getCondition(selfrow.queryRow());
  3699. }
  3700. static CriticalSection ifcs;
  3701. RtlSerialIfBlockTypeInfo::~RtlSerialIfBlockTypeInfo()
  3702. {
  3703. ::Release(filter);
  3704. delete parentRecord;
  3705. }
  3706. void RtlSerialIfBlockTypeInfo::doCleanup() const
  3707. {
  3708. delete parentRecord;
  3709. parentRecord = nullptr;
  3710. ::Release(filter);
  3711. filter = nullptr;
  3712. RtlIfBlockTypeInfo::doCleanup();
  3713. }
  3714. bool RtlSerialIfBlockTypeInfo::getCondition(const byte * selfrow) const
  3715. {
  3716. const IFieldFilter * filter = resolveCondition();
  3717. RtlDynRow row(*parentRecord, nullptr);
  3718. //Can only expand offset for fields up to and including this if block - otherwise it will call getCondition() again...
  3719. row.setRow(selfrow, numPrevFields);
  3720. return filter->matches(row);
  3721. }
  3722. bool RtlSerialIfBlockTypeInfo::getCondition(const RtlRow & selfrow) const
  3723. {
  3724. return resolveCondition()->matches(selfrow);
  3725. }
  3726. const IFieldFilter * RtlSerialIfBlockTypeInfo::queryFilter() const
  3727. {
  3728. return resolveCondition();
  3729. }
  3730. const IFieldFilter * RtlSerialIfBlockTypeInfo::resolveCondition() const
  3731. {
  3732. //The following cast is a hack to avoid <atomic> being included from the header
  3733. //if parentRecord is not initialised then may need to create the filter condition
  3734. typedef std::atomic<RtlRecord *> AtomicRtlRecord;
  3735. AtomicRtlRecord & atomicParentRecord = *reinterpret_cast<AtomicRtlRecord *>(&parentRecord);
  3736. RtlRecord * parent = atomicParentRecord.load(std::memory_order_relaxed);
  3737. if (!parent)
  3738. {
  3739. CriticalBlock block(ifcs);
  3740. parent = atomicParentRecord.load(std::memory_order_relaxed);
  3741. if (!parent)
  3742. {
  3743. if (!filter)
  3744. filter = createCondition();
  3745. parent = new RtlRecord(*rowType, true);
  3746. atomicParentRecord.store(parent, std::memory_order_relaxed);
  3747. numPrevFields = parentRecord->queryIfBlockLimit(this);
  3748. }
  3749. }
  3750. return filter;
  3751. }
  3752. IFieldFilter * RtlDynamicIfBlockTypeInfo::createCondition() const
  3753. {
  3754. //The filter should be initialised on deserialization
  3755. UNIMPLEMENTED;
  3756. }
  3757. //-------------------------------------------------------------------------------------------------------------------
  3758. __int64 RtlBitfieldTypeInfo::signedValue(const void * self) const
  3759. {
  3760. __int64 value = rtlReadInt(self, getBitfieldIntSize());
  3761. unsigned shift = getBitfieldShift();
  3762. unsigned numBits = getBitfieldNumBits();
  3763. unsigned bitsInValue = sizeof(value) * 8;
  3764. value <<= (bitsInValue - shift - numBits);
  3765. return value >> (bitsInValue - numBits);
  3766. }
  3767. unsigned __int64 RtlBitfieldTypeInfo::unsignedValue(const void * self) const
  3768. {
  3769. unsigned __int64 value = rtlReadUInt(self, getBitfieldIntSize());
  3770. unsigned shift = getBitfieldShift();
  3771. unsigned numBits = getBitfieldNumBits();
  3772. unsigned bitsInValue = sizeof(value) * 8;
  3773. value <<= (bitsInValue - shift - numBits);
  3774. return value >> (bitsInValue - numBits);
  3775. }
  3776. size32_t RtlBitfieldTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  3777. {
  3778. builder.ensureCapacity(length+offset, queryName(field));
  3779. byte * cur = builder.getSelf() + offset;
  3780. unsigned __int64 value = rtlReadUInt(builder.getSelf(), getBitfieldIntSize());
  3781. unsigned shift = getBitfieldShift();
  3782. unsigned numBits = getBitfieldNumBits();
  3783. unsigned __int64 mask = (U64C(1) << numBits) - 1;
  3784. value &= ~(mask << shift);
  3785. value |= ((val << shift) & mask);
  3786. rtlWriteInt(cur, val, getBitfieldIntSize());
  3787. return offset + getSize();
  3788. }
  3789. size32_t RtlBitfieldTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  3790. {
  3791. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  3792. }
  3793. size32_t RtlBitfieldTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  3794. {
  3795. size32_t size = rtlUtf8Length(len, value);
  3796. return buildInt(builder, offset, field, rtlStrToInt8(size, value));
  3797. }
  3798. size32_t RtlBitfieldTypeInfo::getMinSize() const
  3799. {
  3800. if (fieldType & RFTMislastbitfield)
  3801. return getBitfieldIntSize();
  3802. return 0;
  3803. }
  3804. size32_t RtlBitfieldTypeInfo::size(const byte * self, const byte * selfrow) const
  3805. {
  3806. if (fieldType & RFTMislastbitfield)
  3807. return getBitfieldIntSize();
  3808. return 0;
  3809. }
  3810. size32_t RtlBitfieldTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3811. {
  3812. if (isUnsigned())
  3813. target.processUInt(unsignedValue(self), field);
  3814. else
  3815. target.processInt(signedValue(self), field);
  3816. return getSize();
  3817. }
  3818. size32_t RtlBitfieldTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3819. {
  3820. size32_t fieldsize = getBitfieldIntSize();
  3821. if (isUnsigned())
  3822. target.outputUInt(unsignedValue(self), fieldsize, queryScalarXPath(field));
  3823. else
  3824. target.outputInt(signedValue(self), fieldsize, queryScalarXPath(field));
  3825. if (fieldType & RFTMislastbitfield)
  3826. return fieldsize;
  3827. return 0;
  3828. }
  3829. void RtlBitfieldTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  3830. {
  3831. if (isUnsigned())
  3832. rtlUInt8ToStrX(resultLen, result, unsignedValue(ptr));
  3833. else
  3834. rtlInt8ToStrX(resultLen, result, signedValue(ptr));
  3835. }
  3836. void RtlBitfieldTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  3837. {
  3838. getString(resultLen, result, ptr);
  3839. }
  3840. __int64 RtlBitfieldTypeInfo::getInt(const void * ptr) const
  3841. {
  3842. if (isUnsigned())
  3843. return (__int64)unsignedValue(ptr);
  3844. else
  3845. return signedValue(ptr);
  3846. }
  3847. int RtlBitfieldTypeInfo::compare(const byte * left, const byte * right) const
  3848. {
  3849. if (isUnsigned())
  3850. {
  3851. unsigned __int64 leftValue = unsignedValue(left);
  3852. unsigned __int64 rightValue = unsignedValue(right);
  3853. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  3854. }
  3855. else
  3856. {
  3857. __int64 leftValue = signedValue(left);
  3858. __int64 rightValue = signedValue(right);
  3859. return (leftValue < rightValue) ? -1 : (leftValue > rightValue) ? +1 : 0;
  3860. }
  3861. }
  3862. unsigned RtlBitfieldTypeInfo::hash(const byte * self, unsigned inhash) const
  3863. {
  3864. __int64 val = getInt(self);
  3865. return rtlHash32Data8(&val, inhash);
  3866. }
  3867. size32_t RtlBitfieldTypeInfo::getSize() const
  3868. {
  3869. if (fieldType & RFTMislastbitfield)
  3870. return getBitfieldIntSize();
  3871. return 0;
  3872. }
  3873. //-------------------------------------------------------------------------------------------------------------------
  3874. size32_t RtlUnimplementedTypeInfo::getMinSize() const
  3875. {
  3876. rtlFailUnexpected();
  3877. }
  3878. size32_t RtlUnimplementedTypeInfo::size(const byte * self, const byte * selfrow) const
  3879. {
  3880. rtlFailUnexpected();
  3881. }
  3882. size32_t RtlUnimplementedTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  3883. {
  3884. rtlFailUnexpected();
  3885. }
  3886. size32_t RtlUnimplementedTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  3887. {
  3888. rtlFailUnexpected();
  3889. }
  3890. size32_t RtlUnimplementedTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3891. {
  3892. rtlFailUnexpected();
  3893. }
  3894. size32_t RtlUnimplementedTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3895. {
  3896. rtlFailUnexpected();
  3897. }
  3898. size32_t RtlUnimplementedTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3899. {
  3900. rtlFailUnexpected();
  3901. return offset;
  3902. }
  3903. void RtlUnimplementedTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3904. {
  3905. rtlFailUnexpected();
  3906. }
  3907. void RtlUnimplementedTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  3908. {
  3909. resultLen = 0;
  3910. result = nullptr;
  3911. rtlFailUnexpected();
  3912. }
  3913. void RtlUnimplementedTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  3914. {
  3915. resultLen = 0;
  3916. result = nullptr;
  3917. rtlFailUnexpected();
  3918. }
  3919. __int64 RtlUnimplementedTypeInfo::getInt(const void * ptr) const
  3920. {
  3921. rtlFailUnexpected();
  3922. return 0;
  3923. }
  3924. int RtlUnimplementedTypeInfo::compare(const byte * left, const byte * right) const
  3925. {
  3926. rtlFailUnexpected();
  3927. }
  3928. unsigned RtlUnimplementedTypeInfo::hash(const byte * self, unsigned inhash) const
  3929. {
  3930. rtlFailUnexpected();
  3931. }
  3932. //-------------------------------------------------------------------------------------------------------------------
  3933. size32_t RtlAlienTypeInfo::getMinSize() const
  3934. {
  3935. rtlFailUnexpected();
  3936. }
  3937. size32_t RtlAlienTypeInfo::size(const byte * self, const byte * selfrow) const
  3938. {
  3939. if (isFixedSize())
  3940. return length;
  3941. rtlFailUnexpected();
  3942. }
  3943. size32_t RtlAlienTypeInfo::buildInt(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, __int64 val) const
  3944. {
  3945. rtlFailUnexpected();
  3946. }
  3947. size32_t RtlAlienTypeInfo::buildString(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t size, const char *value) const
  3948. {
  3949. rtlFailUnexpected();
  3950. }
  3951. size32_t RtlAlienTypeInfo::buildUtf8(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, size32_t len, const char *value) const
  3952. {
  3953. rtlFailUnexpected();
  3954. }
  3955. size32_t RtlAlienTypeInfo::process(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IFieldProcessor & target) const
  3956. {
  3957. rtlFailUnexpected();
  3958. }
  3959. size32_t RtlAlienTypeInfo::toXML(const byte * self, const byte * selfrow, const RtlFieldInfo * field, IXmlWriter & target) const
  3960. {
  3961. rtlFailUnexpected();
  3962. }
  3963. size32_t RtlAlienTypeInfo::deserialize(ARowBuilder & builder, IRowDeserializerSource & in, size32_t offset) const
  3964. {
  3965. if (isFixedSize())
  3966. return RtlCompoundTypeInfo::deserialize(builder, in, offset);
  3967. rtlFailUnexpected();
  3968. }
  3969. void RtlAlienTypeInfo::readAhead(IRowPrefetcherSource & in) const
  3970. {
  3971. if (isFixedSize())
  3972. in.skip(length);
  3973. else
  3974. rtlFailUnexpected();
  3975. }
  3976. void RtlAlienTypeInfo::getString(size32_t & resultLen, char * & result, const void * ptr) const
  3977. {
  3978. resultLen = 0;
  3979. result = nullptr;
  3980. rtlFailUnexpected();
  3981. }
  3982. void RtlAlienTypeInfo::getUtf8(size32_t & resultLen, char * & result, const void * ptr) const
  3983. {
  3984. resultLen = 0;
  3985. result = nullptr;
  3986. rtlFailUnexpected();
  3987. }
  3988. __int64 RtlAlienTypeInfo::getInt(const void * ptr) const
  3989. {
  3990. rtlFailUnexpected();
  3991. return 0;
  3992. }
  3993. int RtlAlienTypeInfo::compare(const byte * left, const byte * right) const
  3994. {
  3995. rtlFailUnexpected();
  3996. }
  3997. unsigned RtlAlienTypeInfo::hash(const byte * self, unsigned inhash) const
  3998. {
  3999. rtlFailUnexpected();
  4000. }
  4001. //-------------------------------------------------------------------------------------------------------------------
  4002. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type, unsigned _flags, const char *_initializer)
  4003. : RtlFieldInfo(_name, _xpath, _type, _flags, _initializer)
  4004. {
  4005. }
  4006. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type, unsigned _flags)
  4007. : RtlFieldInfo(_name, _xpath, _type, _flags, NULL)
  4008. {
  4009. }
  4010. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type)
  4011. : RtlFieldInfo(_name, _xpath, _type, 0, NULL)
  4012. {
  4013. }
  4014. RtlFieldStrInfo::RtlFieldStrInfo(const char * _name, const char * _xpath, const RtlTypeInfo * _type, const char *_initializer)
  4015. : RtlFieldInfo(_name, _xpath, _type, 0, _initializer)
  4016. {
  4017. }
  4018. unsigned ECLRTL_API countFields(const RtlFieldInfo * const * fields)
  4019. {
  4020. unsigned cnt = 0;
  4021. for (;*fields;fields++)
  4022. cnt++;
  4023. return cnt;
  4024. }
  4025. //-------------------------------------------------------------------------------------------------------------------
  4026. static const unsigned sameTypeMask = RFTMkind|RFTMebcdic;
  4027. size32_t translateScalar(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, const RtlTypeInfo &destType, const RtlTypeInfo &sourceType, const byte *source)
  4028. {
  4029. switch(destType.getType())
  4030. {
  4031. case type_boolean:
  4032. case type_int:
  4033. case type_swapint:
  4034. case type_packedint:
  4035. case type_filepos:
  4036. case type_keyedint:
  4037. offset = destType.buildInt(builder, offset, field, sourceType.getInt(source));
  4038. break;
  4039. case type_real:
  4040. offset = destType.buildReal(builder, offset, field, sourceType.getReal(source));
  4041. break;
  4042. case type_data:
  4043. case type_string:
  4044. //Special case if source and destination types are identical to avoid cloning strings
  4045. if ((destType.fieldType & sameTypeMask) == (sourceType.fieldType & sameTypeMask))
  4046. {
  4047. size32_t length = sourceType.length;
  4048. if (!sourceType.isFixedSize())
  4049. {
  4050. length = rtlReadSize32t(source);
  4051. source += sizeof(size32_t);
  4052. }
  4053. offset = destType.buildString(builder, offset, field, length, (const char *)source);
  4054. break;
  4055. }
  4056. //fallthrough
  4057. case type_decimal: // Go via string - not common enough to special-case
  4058. case type_varstring:
  4059. case type_qstring:
  4060. {
  4061. size32_t size;
  4062. rtlDataAttr text;
  4063. sourceType.getString(size, text.refstr(), source);
  4064. offset = destType.buildString(builder, offset, field, size, text.getstr());
  4065. break;
  4066. }
  4067. case type_utf8:
  4068. //MORE: Could special case casting from utf8 to utf8 similar to strings above
  4069. case type_unicode:
  4070. case type_varunicode:
  4071. {
  4072. size32_t utf8chars;
  4073. rtlDataAttr utf8Text;
  4074. sourceType.getUtf8(utf8chars, utf8Text.refstr(), source);
  4075. offset = destType.buildUtf8(builder, offset, field, utf8chars, utf8Text.getstr());
  4076. break;
  4077. }
  4078. case type_set:
  4079. {
  4080. bool isAll = *(bool *) source;
  4081. source+= sizeof(bool);
  4082. byte *dest = builder.ensureCapacity(offset+sizeof(bool)+sizeof(size32_t), field->name)+offset;
  4083. *(size32_t *) (dest + sizeof(bool)) = 0; // Patch later when size known
  4084. offset += sizeof(bool) + sizeof(size32_t);
  4085. if (isAll)
  4086. {
  4087. *(bool*) dest = true;
  4088. }
  4089. else
  4090. {
  4091. *(bool*) dest = false;
  4092. size32_t sizeOffset = offset - sizeof(size32_t); // Where we need to patch
  4093. size32_t childSize = *(size32_t *)source;
  4094. source += sizeof(size32_t);
  4095. const byte *initialSource = source;
  4096. size32_t initialOffset = offset;
  4097. const RtlTypeInfo *destChildType = destType.queryChildType();
  4098. const RtlTypeInfo *sourceChildType = sourceType.queryChildType();
  4099. while ((size_t)(source - initialSource) < childSize)
  4100. {
  4101. offset = translateScalar(builder, offset, field, *destChildType, *sourceChildType, source);
  4102. source += sourceChildType->size(source, nullptr); // MORE - shame to repeat a calculation that the translate above almost certainly just did
  4103. }
  4104. dest = builder.getSelf() + sizeOffset; // Note - man have been moved by reallocs since last calculated
  4105. *(size32_t *)dest = offset - initialOffset;
  4106. }
  4107. break;
  4108. }
  4109. default:
  4110. throwUnexpected();
  4111. }
  4112. return offset;
  4113. }
  4114. //-------------------------------------------------------------------------------------------------------------------
  4115. /*
  4116. Stack:
  4117. * Change hqlhtcpp so that the correct derived classes are generated.
  4118. * Test so that toXML calls the default implementaions and check that the same values are generated. (Don't if contains ifblocks/alien)
  4119. * Release
  4120. * Think about bitfields - how do I know it is the last bitfield, how am I going to keep track of the offsets.
  4121. * What code would need to be generated for alien datatypes.
  4122. * Could have alien int and alien string varieties????
  4123. * What would an ecl interpreter look like (a special workunit?) helpers are interpreted? What about the graph?
  4124. * Could I add associations to register user attributes - so a callback could know when they were assigned to?
  4125. * Could I add ctx->noteLocation() into the generated code - so could put breakpoints on variables.
  4126. * Add annotation when a member of the target dataset is updated.
  4127. ctx->noteFieldAssigned(self, <field>); - does this include temporary datasets?
  4128. ctx->noteAttributeX(<name>, Int|String|Unicode|
  4129. ctx->noteLocation(location); - reduce locations, so only one returned per line for a non-dataset? Should it just be the first item on the line that is tagged??
  4130. * Need static information about the breakpoints so debugger knows where to put valid brakpoints....
  4131. * Debugger will want to know about the type of the breakpoints.
  4132. * Should try and compress the location format - possibly have a table of <module.attributes>-># with breakpoint as 12:23
  4133. Also need some information about which datasets, and stored variables etc. are used so they can be displayed.
  4134. - Most datasets can be deduced from the parameters passed into the transform
  4135. - Some are trickier e.g., the extract, could possibly define some mappings
  4136. - options to disable projects/other more complex operations inline (so easier to walk through)
  4137. Bitfields:
  4138. - Two separate questions:
  4139. i) How is the meta information generated.
  4140. ii) How is it stored internally in an IHqlExpression * ?
  4141. * Could store the offset in the type - either in the base type of as a qualifier.
  4142. + much easier code generation.
  4143. - Doesn't provie an easy indication of the last field in a bitfield (because can't really modify after the fact)
  4144. - Problematic when fields are removed to merge them.
  4145. * Could add a bitfield container to the record.
  4146. + Makes it easier to handle the last bitfield
  4147. + Matches the structure used for the cursor.
  4148. - Everything needs to walk the bitfield containers similar to ifblocks.
  4149. - Makes it just as tricky to merge
  4150. - Harder to create the record, unless the code is implicitly handled by appendOperand().
  4151. * The type of no_select could contain a modifier to indicate the offset/islast
  4152. + the type of a no_select would have a 1:1 mapping with type info.
  4153. - A bit complicated to calculate, especially when it isn't used much of the time
  4154. => On Reflection is is probably easiest to keep the structure as it is (some comments should go in hqlexpr to avoid revisiting).
  4155. * interperet bitfield offsets and "is last bitfield" dynamically
  4156. + Greatly simplifies generating the meta - you can always use the field.
  4157. - Requires another parameter and significant extra complexity for the uncommon case. (especially incrementing self)
  4158. * Could generate from the expanded record instead of walking the record structure directly
  4159. + That already knows how the bitfields are allocated, and could easily know which is the last field.
  4160. - A field is no longer sufficient as key fr searching for the information.
  4161. - Best would be a createFieldTypeKey(select-expr) which returns field when approriate, or modified if a bitfield. Then the pain is localised.
  4162. * Output a bitfield container item into the type information
  4163. + Solves the size problem
  4164. - Individual bitfields still need to know their offsets, so doesn't solve the full problem.
  4165. =>
  4166. Change so that either use meta to generate the information, or use no_select when appropriate to fidn out the nesc. information.
  4167. Probably the latter for the moment.
  4168. a) Create a key function and make sure it is always used.
  4169. b) Need to work out how to generate no_ifblock.
  4170. - ifblock is context dependent, so need to generate as part of the parent record, and in the parent record context.
  4171. */