rtldynfield.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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 "jmisc.hpp"
  17. #include "jlib.hpp"
  18. #include "eclhelper.hpp"
  19. #include "eclrtl_imp.hpp"
  20. #include "rtldynfield.hpp"
  21. #include "rtlrecord.hpp"
  22. #include "rtlembed.hpp"
  23. #include "rtlnewkey.hpp"
  24. //#define TRACE_TRANSLATION
  25. #define VALIDATE_TYPEINFO_HASHES
  26. #define RTLTYPEINFO_FORMAT_1 80 // In case we ever want to support more than one format
  27. //---------------------------------------------------------------------------------------------------------------------
  28. extern ECLRTL_API RecordTranslationMode getTranslationMode(const char *val)
  29. {
  30. if (strieq(val, "alwaysDisk"))
  31. return RecordTranslationMode::AlwaysDisk;
  32. else if (strieq(val, "alwaysECL"))
  33. return RecordTranslationMode::AlwaysECL;
  34. else if (!val || strToBool(val) || strieq(val, "payload"))
  35. return RecordTranslationMode::Payload;
  36. else
  37. return RecordTranslationMode::None;
  38. }
  39. extern ECLRTL_API const char *getTranslationModeText(RecordTranslationMode val)
  40. {
  41. switch (val)
  42. {
  43. case RecordTranslationMode::AlwaysDisk: return "alwaysDisk";
  44. case RecordTranslationMode::AlwaysECL: return "alwaysECL";
  45. case RecordTranslationMode::Payload: return "payload";
  46. case RecordTranslationMode::None: return "off";
  47. }
  48. throwUnexpected();
  49. }
  50. //---------------------------------------------------------------------------------------------------------------------
  51. const RtlTypeInfo *FieldTypeInfoStruct::createRtlTypeInfo(IThorIndexCallback *_callback) const
  52. {
  53. const RtlTypeInfo *ret = nullptr;
  54. switch (fieldType & RFTMkind)
  55. {
  56. case type_boolean:
  57. ret = new RtlBoolTypeInfo(fieldType, length);
  58. break;
  59. case type_keyedint:
  60. ret = new RtlKeyedIntTypeInfo(fieldType, length, childType);
  61. break;
  62. case type_blob: // MORE - will need its own type (see code below)
  63. case type_int:
  64. ret = new RtlIntTypeInfo(fieldType, length);
  65. break;
  66. #if 0 // Later when implemented
  67. case type_blob:
  68. ret = new RtlBlobTypeInfo(fieldType, length, childType, _callback);
  69. break;
  70. #endif
  71. case type_filepos:
  72. #if __BYTE_ORDER == __LITTLE_ENDIAN
  73. ret = new RtlSwapIntTypeInfo(fieldType, length);
  74. #else
  75. ret = new RtlIntTypeInfo(fieldType, length);
  76. #endif
  77. break;
  78. case type_real:
  79. ret = new RtlRealTypeInfo(fieldType, length);
  80. break;
  81. case type_decimal:
  82. ret = new RtlDecimalTypeInfo(fieldType, length);
  83. break;
  84. case type_string:
  85. ret = new RtlStringTypeInfo(fieldType, length);
  86. break;
  87. case type_bitfield:
  88. ret = new RtlBitfieldTypeInfo(fieldType, length);
  89. break;
  90. case type_varstring:
  91. ret = new RtlVarStringTypeInfo(fieldType, length);
  92. break;
  93. case type_data:
  94. ret = new RtlDataTypeInfo(fieldType, length);
  95. break;
  96. case type_table:
  97. assert(childType);
  98. ret = new RtlDatasetTypeInfo(fieldType, length, childType);
  99. break;
  100. case type_dictionary:
  101. assert(childType);
  102. ret = new RtlDictionaryTypeInfo(fieldType, length, childType);
  103. break;
  104. case type_set:
  105. assert(childType);
  106. ret = new RtlSetTypeInfo(fieldType, length, childType);
  107. break;
  108. case type_row:
  109. assert(childType);
  110. ret = new RtlRowTypeInfo(fieldType, length, childType);
  111. break;
  112. case type_swapint:
  113. ret = new RtlSwapIntTypeInfo(fieldType, length);
  114. break;
  115. case type_packedint:
  116. ret = new RtlPackedIntTypeInfo(fieldType, length);
  117. break;
  118. case type_qstring:
  119. ret = new RtlQStringTypeInfo(fieldType, length);
  120. break;
  121. case type_unicode:
  122. ret = new RtlUnicodeTypeInfo(fieldType, length, locale);
  123. break;
  124. case type_varunicode:
  125. ret = new RtlVarUnicodeTypeInfo(fieldType, length, locale);
  126. break;
  127. case type_utf8:
  128. ret = new RtlUtf8TypeInfo(fieldType, length, locale);
  129. break;
  130. case type_record:
  131. ret = new RtlRecordTypeInfo(fieldType, length, fieldsArray);
  132. break;
  133. case type_ifblock:
  134. ret = new RtlDynamicIfBlockTypeInfo(fieldType, length, fieldsArray, nullptr, filter);
  135. break;
  136. case type_alien:
  137. assert(childType);
  138. ret = new RtlAlienTypeInfo(fieldType, length, childType);
  139. break;
  140. default:
  141. throwUnexpected();
  142. }
  143. return ret;
  144. };
  145. typedef MapBetween<const RtlTypeInfo *, const RtlTypeInfo *, StringAttr, const char *> TypeNameMap;
  146. typedef MapBetween<const RtlTypeInfo *, const RtlTypeInfo *, unsigned, unsigned> TypeNumMap;
  147. /**
  148. * class CRtlFieldTypeSerializer
  149. *
  150. * Serializer class for creating json representation of a RtlTypeInfo structure.
  151. *
  152. */
  153. class CRtlFieldTypeSerializer
  154. {
  155. public:
  156. /**
  157. * Serialize a RtlTypeInfo structure to JSON
  158. *
  159. * @param out Buffer for resulting serialized string
  160. * @param type RtlTypeInfo structure to be serialized
  161. * @return Referenced to supplied buffer
  162. */
  163. static StringBuffer &serialize(StringBuffer &out, const RtlTypeInfo *type)
  164. {
  165. CRtlFieldTypeSerializer s(out, type);
  166. s.doSerialize();
  167. return out;
  168. }
  169. private:
  170. CRtlFieldTypeSerializer(StringBuffer &_out, const RtlTypeInfo *_base)
  171. : json(_out), base(_base)
  172. {
  173. }
  174. void doSerialize()
  175. {
  176. json.append("{");
  177. serializeType(base);
  178. json.append("\n}");
  179. }
  180. void serializeType(const RtlTypeInfo *type)
  181. {
  182. if (!serialized(type))
  183. {
  184. // Make sure all child types are serialized first
  185. const RtlTypeInfo *childType = type->queryChildType();
  186. if (childType)
  187. serializeType(childType);
  188. const RtlFieldInfo * const * fields = type->queryFields();
  189. if (fields)
  190. {
  191. for (;;)
  192. {
  193. const RtlFieldInfo * child = *fields;
  194. if (!child)
  195. break;
  196. serializeType(child->type);
  197. fields++;
  198. }
  199. }
  200. // Now serialize this one
  201. if (type != base)
  202. {
  203. VStringBuffer newName("ty%d", ++nextTypeName);
  204. types.setValue(type, newName.str());
  205. startField(newName.str());
  206. serializeMe(type);
  207. closeCurly();
  208. }
  209. else
  210. serializeMe(type);
  211. }
  212. }
  213. void serializeMe(const RtlTypeInfo *type)
  214. {
  215. if (!type->canSerialize())
  216. throw makeStringException(MSGAUD_user, 1000, "This type structure cannot be serialized");
  217. addPropHex("fieldType", type->fieldType);
  218. addProp("length", type->length);
  219. addPropNonEmpty("locale", type->queryLocale());
  220. const RtlTypeInfo *childType = type->queryChildType();
  221. if (childType)
  222. addPropType("child", childType);
  223. const IFieldFilter * filter = type->queryFilter();
  224. if (filter)
  225. {
  226. StringBuffer filterText;
  227. filter->serialize(filterText);
  228. addProp("filterField", filter->queryFieldIndex());
  229. addPropType("filterType", &filter->queryType());
  230. addProp("filter", filterText);
  231. }
  232. const RtlFieldInfo * const * fields = type->queryFields();
  233. if (fields)
  234. {
  235. startFields();
  236. for (;;)
  237. {
  238. const RtlFieldInfo * child = *fields;
  239. if (!child)
  240. break;
  241. newline();
  242. openCurly();
  243. addProp("name", child->name);
  244. addPropType("type", child->type);
  245. addProp("xpath", child->xpath);
  246. if (child->flags)
  247. addPropHex("flags", child->flags);
  248. // initializer is tricky - it's not (in general) a null-terminated string but the actual length is not easily available
  249. if (child->initializer)
  250. {
  251. addProp("init", child->type->size(child->initializer, nullptr), child->initializer);
  252. }
  253. closeCurly();
  254. fields++;
  255. }
  256. endFields();
  257. }
  258. }
  259. bool serialized(const RtlTypeInfo *type)
  260. {
  261. return types.find(type) != nullptr;
  262. }
  263. void startField(const char *name)
  264. {
  265. newline().appendf("\"%s\": ", name);
  266. openCurly();
  267. }
  268. void addProp(const char *propName, const char *propVal)
  269. {
  270. if (propVal)
  271. {
  272. newline();
  273. encodeJSON(json.append("\""), propName).append("\": ");
  274. encodeJSON(json.append("\""), propVal).append("\"");
  275. }
  276. }
  277. void addProp(const char *propName, size32_t propLen, const byte *propVal)
  278. {
  279. if (propVal)
  280. {
  281. newline();
  282. encodeJSON(json.append("\""), propName).append("\": \"");
  283. JBASE64_Encode(propVal, propLen, json, false);
  284. json.append("\"");
  285. }
  286. }
  287. void addPropNonEmpty(const char *propName, const char *propVal)
  288. {
  289. if (propVal && *propVal)
  290. addProp(propName, propVal);
  291. }
  292. void addProp(const char *propName, unsigned propVal)
  293. {
  294. newline().appendf("\"%s\": %u", propName, propVal);
  295. }
  296. void addPropHex(const char *propName, unsigned propVal)
  297. {
  298. newline().appendf("\"%s\": %u", propName, propVal); // Nice idea but json does not support hex constants :(
  299. }
  300. void addPropType(const char *propName, const RtlTypeInfo *type)
  301. {
  302. addProp(propName, queryTypeName(type));
  303. }
  304. const char *queryTypeName(const RtlTypeInfo *type)
  305. {
  306. StringAttr *typeName = types.getValue(type);
  307. assertex(typeName);
  308. return typeName->get();
  309. }
  310. void startFields()
  311. {
  312. newline().appendf("\"fields\": ");
  313. openCurly('[');
  314. }
  315. void endFields()
  316. {
  317. closeCurly(']');
  318. }
  319. StringBuffer &newline()
  320. {
  321. if (commaPending)
  322. json.append(',');
  323. json.appendf("\n%*s", indent, "");
  324. commaPending = true;
  325. return json;
  326. }
  327. void closeCurly(char brace = '}')
  328. {
  329. indent--;
  330. json.appendf("\n%*s%c", indent, "", brace);
  331. commaPending = true;
  332. }
  333. void openCurly(char brace = '{')
  334. {
  335. json.append(brace);
  336. indent++;
  337. commaPending = false;
  338. }
  339. TypeNameMap types;
  340. StringBuffer &json;
  341. const RtlTypeInfo *base = nullptr;
  342. unsigned indent = 1;
  343. unsigned nextTypeName = 0;
  344. bool commaPending = false;
  345. };
  346. class CRtlFieldTypeBinSerializer
  347. {
  348. public:
  349. /**
  350. * Serialize a RtlTypeInfo structure to binary
  351. *
  352. * @param out Buffer for resulting serialized string
  353. * @param type RtlTypeInfo structure to be serialized
  354. * @return Referenced to supplied buffer
  355. */
  356. static MemoryBuffer &serialize(MemoryBuffer &out, const RtlTypeInfo *type)
  357. {
  358. int oldEnd = out.setEndian(__LITTLE_ENDIAN);
  359. CRtlFieldTypeBinSerializer s(out);
  360. byte format = RTLTYPEINFO_FORMAT_1;
  361. out.append(format);
  362. DelayedMarker<hash64_t> hash(out);
  363. DelayedSizeMarker size(out);
  364. size32_t pos = out.length();
  365. s.serializeType(type);
  366. size.write();
  367. hash.write(rtlHash64Data(size.size(), out.toByteArray()+pos, 0));
  368. out.setEndian(oldEnd);
  369. return out;
  370. }
  371. private:
  372. CRtlFieldTypeBinSerializer(MemoryBuffer &_out)
  373. : out(_out)
  374. {
  375. }
  376. void serializeType(const RtlTypeInfo *type)
  377. {
  378. if (!serialized(type))
  379. {
  380. // Make sure all child types are serialized first
  381. const RtlTypeInfo *child = type->queryChildType();
  382. if (child)
  383. serializeType(child);
  384. const RtlFieldInfo * const * fields = type->queryFields();
  385. if (fields)
  386. {
  387. for (unsigned idx = 0;;idx++)
  388. {
  389. const RtlFieldInfo * child = fields[idx];
  390. if (!child)
  391. break;
  392. serializeType(child->type);
  393. }
  394. }
  395. // Now serialize this one
  396. types.setValue(type, nextTypeNum++);
  397. serializeMe(type);
  398. }
  399. }
  400. void serializeMe(const RtlTypeInfo *type)
  401. {
  402. if (!type->canSerialize())
  403. throw makeStringException(MSGAUD_user, 1000, "This type structure cannot be serialized");
  404. unsigned fieldType = type->fieldType;
  405. const char *locale = type->queryLocale();
  406. if (locale && *locale)
  407. fieldType |= RFTMhasLocale;
  408. const RtlTypeInfo *child = type->queryChildType();
  409. if (child)
  410. fieldType |= RFTMhasChildType;
  411. const RtlFieldInfo * const * fields = type->queryFields();
  412. if (fields)
  413. fieldType |= RFTMhasFields;
  414. out.append(fieldType);
  415. out.appendPacked(type->length);
  416. if (fieldType & RFTMhasLocale)
  417. out.append(locale);
  418. if (child)
  419. out.appendPacked(queryTypeIdx(child));
  420. const IFieldFilter * filter = type->queryFilter();
  421. if (filter)
  422. {
  423. out.appendPacked(filter->queryFieldIndex());
  424. out.appendPacked(queryTypeIdx(&filter->queryType()));
  425. filter->serialize(out);
  426. }
  427. if (fields)
  428. {
  429. unsigned count = countFields(fields);
  430. out.appendPacked(count);
  431. for (;;)
  432. {
  433. const RtlFieldInfo * child = *fields;
  434. if (!child)
  435. break;
  436. out.append(child->name);
  437. out.appendPacked(queryTypeIdx(child->type));
  438. unsigned flags = child->flags;
  439. if (child->xpath)
  440. flags |= RFTMhasXpath;
  441. if (child->initializer)
  442. flags |= RFTMhasInitializer;
  443. out.append(flags);
  444. if (child->xpath)
  445. out.append(child->xpath);
  446. // initializer is tricky - it's not (in general) a null-terminated string but the actual length is not easily available
  447. if (child->initializer)
  448. {
  449. unsigned initLength = child->type->size(child->initializer, nullptr);
  450. out.appendPacked(initLength).append(initLength, child->initializer);
  451. }
  452. fields++;
  453. }
  454. }
  455. }
  456. bool serialized(const RtlTypeInfo *type)
  457. {
  458. return types.find(type) != nullptr;
  459. }
  460. unsigned queryTypeIdx(const RtlTypeInfo *type)
  461. {
  462. unsigned *typeNum = types.getValue(type);
  463. assertex(typeNum);
  464. return *typeNum;
  465. }
  466. TypeNumMap types;
  467. MemoryBuffer &out;
  468. unsigned nextTypeNum = 0;
  469. };
  470. /**
  471. * class CRtlFieldTypeDeserializer
  472. *
  473. * Deserializer class for creating a RtlTypeInfo structure from json representation.
  474. *
  475. * Note that the resulting RtlTypeInfo structures are owned by this object and will be
  476. * destroyed when this object is destroyed.
  477. *
  478. */
  479. class CRtlFieldTypeDeserializer : public CInterfaceOf<IRtlFieldTypeDeserializer>
  480. {
  481. public:
  482. /**
  483. * CRtlFieldTypeDeserializer constructor
  484. *
  485. * @param _callback Supplies a callback to be used for blobs/filepositions.
  486. */
  487. CRtlFieldTypeDeserializer(IThorIndexCallback *_callback)
  488. : callback(_callback)
  489. {
  490. }
  491. /**
  492. * CRtlFieldTypeDeserializer destructor
  493. * <p>
  494. * Releases all RtlTypeInfo and related structures created by this deserializer
  495. */
  496. ~CRtlFieldTypeDeserializer()
  497. {
  498. // Need some care - all the RtlTypeInfo objects I created need to be destroyed, together with anything else I had to create
  499. // Strings (other than the init strings) are preserved in the AtomTable
  500. // First allow the types to clean up any critical cached information, then delete them in a second pass
  501. HashIterator allTypes(types);
  502. ForEach(allTypes)
  503. {
  504. const RtlTypeInfo **type = types.mapToValue(&allTypes.query());
  505. cleanupType(*type);
  506. }
  507. cleanupType(base);
  508. ForEach(allTypes)
  509. {
  510. const RtlTypeInfo **type = types.mapToValue(&allTypes.query());
  511. deleteType(*type);
  512. }
  513. deleteType(base);
  514. }
  515. /**
  516. * Obtain the deserialized type information
  517. * <p>
  518. * Note that the RtlTypeInfo objects are not link-counted, so the lifetime of these objects
  519. * is determined by the lifetime of the deserializer. They will be released once the deserializer
  520. * that created them is deleted.
  521. * <p>
  522. * Do not call more than once.
  523. *
  524. * @param _json JSON text to be deserialized, as created by CRtlFieldTypeSerializer
  525. * @return Deserialized type object
  526. */
  527. virtual const RtlTypeInfo *deserialize(const char *json) override
  528. {
  529. assertex(!base);
  530. Owned<IPropertyTree> jsonTree = createPTreeFromJSONString(json);
  531. base = deserializeType(jsonTree, jsonTree);
  532. return base;
  533. }
  534. /**
  535. * Obtain the deserialized type information
  536. * <p>
  537. * Note that the RtlTypeInfo objects are not link-counted, so the lifetime of these objects
  538. * is determined by the lifetime of the deserializer. They will be released once the deserializer
  539. * that created them is deleted.
  540. * <p>
  541. * Do not call more than once.
  542. *
  543. * @param _jsonTree JSON property tree to be deserialized, as created by CRtlFieldTypeSerializer
  544. * @return Deserialized type object
  545. */
  546. virtual const RtlTypeInfo *deserialize(IPropertyTree &jsonTree) override
  547. {
  548. assertex(!base);
  549. base = deserializeType(&jsonTree, &jsonTree);
  550. return base;
  551. }
  552. /**
  553. * Obtain the deserialized type information
  554. * <p>
  555. * Note that the RtlTypeInfo objects are not link-counted, so the lifetime of these objects
  556. * is determined by the lifetime of the deserializer. They will be released once the deserializer
  557. * that created them is deleted.
  558. * <p>
  559. * Do not call more than once.
  560. *
  561. * @param buf Binary serialized typeinfo to be deserialized, as created by CRtlFieldTypeSerializer
  562. * @return Deserialized type object
  563. */
  564. virtual const RtlTypeInfo *deserialize(MemoryBuffer &buf) override
  565. {
  566. assertex(!base);
  567. unsigned nextTypeNum = 0;
  568. int oldEndian = buf.setEndian(__LITTLE_ENDIAN);
  569. try
  570. {
  571. byte format;
  572. buf.read(format);
  573. if (format != RTLTYPEINFO_FORMAT_1)
  574. throw MakeStringException(0, "Invalid type info (%d) in CRtlFieldTypeDeserializer::deserialize", format);
  575. hash64_t hash;
  576. buf.read(hash);
  577. size32_t size;
  578. buf.read(size);
  579. #ifdef VALIDATE_TYPEINFO_HASHES
  580. hash64_t expected = rtlHash64Data(size, buf.readDirect(0), 0);
  581. if (expected != hash)
  582. throw MakeStringException(0, "Invalid type info hash in CRtlFieldTypeDeserializer::deserialize");
  583. #endif
  584. size32_t endpos = buf.getPos() + size;
  585. while (buf.getPos() < endpos)
  586. {
  587. if (base)
  588. {
  589. addType(base, nextTypeNum++);
  590. base = nullptr; // in case of exceptions...
  591. }
  592. base = deserializeType(buf);
  593. }
  594. if (buf.getPos()!=endpos)
  595. throw MakeStringException(0, "Invalid type info (incorrect size data) in CRtlFieldTypeDeserializer::deserialize");
  596. buf.setEndian(oldEndian);
  597. return base;
  598. }
  599. catch(...)
  600. {
  601. buf.setEndian(oldEndian);
  602. throw;
  603. }
  604. }
  605. virtual const RtlTypeInfo *addType(FieldTypeInfoStruct &info, const IInterface *typeOrIfblock) override
  606. {
  607. VStringBuffer name("%p", typeOrIfblock);
  608. const RtlTypeInfo ** found = types.getValue(name);
  609. if (found)
  610. return *found;
  611. savedTypes.append(LINK(typeOrIfblock));
  612. info.locale = keep(info.locale);
  613. const RtlTypeInfo * ret = info.createRtlTypeInfo(callback);
  614. types.setValue(name, ret);
  615. unsigned baseType = (info.fieldType & RFTMkind);
  616. if (baseType == type_record)
  617. patchIfBlockParentRow(ret, static_cast<const RtlRecordTypeInfo *>(ret));
  618. return ret;
  619. }
  620. virtual const RtlTypeInfo *lookupType(const IInterface * typeOrIfBlock) const override
  621. {
  622. VStringBuffer name("%p", typeOrIfBlock);
  623. const RtlTypeInfo ** found = types.getValue(name);
  624. if (found)
  625. return *found;
  626. return nullptr;
  627. }
  628. virtual const RtlFieldInfo *addFieldInfo(const char *fieldName, const char *xpath, const RtlTypeInfo *type, unsigned flags, const char *init) override
  629. {
  630. // MORE - we could hang onto this for cleanup, rather than assuming that we keep it via a later addType() call?
  631. return new RtlFieldStrInfo(keep(fieldName), keep(xpath), type, flags, init);
  632. }
  633. private:
  634. KeptAtomTable atoms; // Used to ensure proper lifetime of strings used in type structures
  635. MapStringTo<const RtlTypeInfo *> types; // Ensures structures only generated once
  636. const RtlTypeInfo *base = nullptr; // Holds the resulting type
  637. IThorIndexCallback *callback = nullptr;
  638. IConstPointerArray savedTypes; // ensure types remain alive for subsequent lookups
  639. void deleteType(const RtlTypeInfo *type)
  640. {
  641. if (type)
  642. {
  643. // Releases all memory for a single RtlTypeInfo object
  644. const RtlFieldInfo * const * fields = type->queryFields();
  645. if (fields)
  646. {
  647. const RtlFieldInfo * const * cur = fields;
  648. for (;;)
  649. {
  650. const RtlFieldInfo * child = *cur;
  651. if (!child)
  652. break;
  653. // We don't need to delete other strings - they are owned by atom table.
  654. // But the initializer is decoded and thus owned by me
  655. free((void *)child->initializer);
  656. delete child;
  657. cur++;
  658. }
  659. delete [] fields;
  660. }
  661. type->doDelete();
  662. }
  663. }
  664. void cleanupType(const RtlTypeInfo *type)
  665. {
  666. if (type)
  667. type->doCleanup();
  668. }
  669. const RtlTypeInfo *lookupType(const char *name, IPropertyTree *all)
  670. {
  671. const RtlTypeInfo ** found = types.getValue(name);
  672. if (found)
  673. return *found;
  674. const RtlTypeInfo *type = deserializeType(all->queryPropTree(name), all);
  675. types.setValue(name, type);
  676. return type;
  677. }
  678. const RtlTypeInfo *lookupType(unsigned idx)
  679. {
  680. // Could keep an expanding array of types instead - but the hash table is already there for json support...
  681. VStringBuffer key("%u", idx);
  682. const RtlTypeInfo ** found = types.getValue(key);
  683. if (found)
  684. return *found;
  685. throw makeStringException(-1, "Invalid serialized type information");
  686. }
  687. void addType(const RtlTypeInfo *type, unsigned idx)
  688. {
  689. VStringBuffer key("%u", idx);
  690. assert(types.getValue(key)==nullptr);
  691. types.setValue(key, type);
  692. }
  693. const char *keep(const char *string)
  694. {
  695. if (string)
  696. return str(atoms.addAtom(string));
  697. else
  698. return nullptr;
  699. }
  700. const RtlTypeInfo *deserializeType(IPropertyTree *type, IPropertyTree *all)
  701. {
  702. FieldTypeInfoStruct info;
  703. info.fieldType = type->getPropInt("fieldType");
  704. info.length = type->getPropInt("length");
  705. info.locale = keep(type->queryProp("locale"));
  706. const char *child = type->queryProp("child");
  707. if (child)
  708. info.childType = lookupType(child, all);
  709. unsigned baseType = (info.fieldType & RFTMkind);
  710. if ((baseType == type_record) || (baseType == type_ifblock))
  711. {
  712. unsigned numFields = type->getCount("fields");
  713. info.fieldsArray = new const RtlFieldInfo * [numFields+1];
  714. info.fieldsArray[numFields] = nullptr;
  715. Owned<IPropertyTreeIterator> fields = type->getElements("fields");
  716. unsigned n = 0;
  717. ForEach(*fields)
  718. {
  719. IPropertyTree &field = fields->query();
  720. const char *fieldTypeName = field.queryProp("type");
  721. const char *fieldName = keep(field.queryProp("name"));
  722. const char *fieldXpath = keep(field.queryProp("xpath"));
  723. unsigned flags = field.getPropInt("flags");
  724. const char *fieldInit = field.queryProp("init");
  725. if (fieldInit)
  726. {
  727. StringBuffer decoded;
  728. JBASE64_Decode(fieldInit, decoded);
  729. fieldInit = decoded.detach(); // NOTE - this gets freed in cleanupType()
  730. }
  731. info.fieldsArray[n] = new RtlFieldStrInfo(fieldName, fieldXpath, lookupType(fieldTypeName, all), flags, fieldInit);
  732. n++;
  733. }
  734. }
  735. if (baseType == type_ifblock)
  736. {
  737. unsigned fieldId = type->getPropInt("filterField");
  738. const RtlTypeInfo * fieldType = lookupType(type->queryProp("filterType"), all);
  739. info.filter = deserializeFieldFilter(fieldId, *fieldType, type->queryProp("filter"));
  740. }
  741. const RtlTypeInfo * result = info.createRtlTypeInfo(callback);
  742. if (baseType == type_record)
  743. patchIfBlockParentRow(result, static_cast<const RtlRecordTypeInfo *>(result));
  744. return result;
  745. }
  746. const RtlTypeInfo *deserializeType(MemoryBuffer &type)
  747. {
  748. FieldTypeInfoStruct info;
  749. type.read(info.fieldType);
  750. type.readPacked(info.length);
  751. if (info.fieldType & RFTMhasLocale)
  752. {
  753. const char *locale;
  754. type.read(locale);
  755. info.locale = keep(locale);
  756. }
  757. if (info.fieldType & RFTMhasChildType)
  758. {
  759. unsigned childIdx;
  760. type.readPacked(childIdx);
  761. info.childType = lookupType(childIdx);
  762. }
  763. unsigned baseType = (info.fieldType & RFTMkind);
  764. if (baseType == type_ifblock)
  765. {
  766. unsigned fieldId;
  767. type.readPacked(fieldId);
  768. unsigned childIdx;
  769. type.readPacked(childIdx);
  770. const RtlTypeInfo * fieldType = lookupType(childIdx);
  771. info.filter = deserializeFieldFilter(fieldId, *fieldType, type);
  772. }
  773. if (info.fieldType & RFTMhasFields)
  774. {
  775. unsigned numFields;
  776. type.readPacked(numFields);
  777. info.fieldsArray = new const RtlFieldInfo * [numFields+1];
  778. info.fieldsArray[numFields] = nullptr;
  779. for (int n = 0; n < numFields; n++)
  780. {
  781. const char *fieldName;
  782. type.read(fieldName);
  783. if (fieldName[0] == '\0')
  784. fieldName = nullptr;
  785. unsigned fieldType;
  786. type.readPacked(fieldType);
  787. unsigned fieldFlags;
  788. type.read(fieldFlags);
  789. const char *xpath = nullptr;
  790. if (fieldFlags & RFTMhasXpath)
  791. type.read(xpath);
  792. void *init = nullptr;
  793. if (fieldFlags & RFTMhasInitializer)
  794. {
  795. unsigned initLength;
  796. type.readPacked(initLength);
  797. init = malloc(initLength);
  798. memcpy(init, type.readDirect(initLength), initLength);
  799. }
  800. fieldFlags &= ~RFTMserializerFlags;
  801. info.fieldsArray[n] = new RtlFieldStrInfo(keep(fieldName), keep(xpath), lookupType(fieldType), fieldFlags, (const char *) init);
  802. }
  803. }
  804. info.fieldType &= ~RFTMserializerFlags;
  805. const RtlTypeInfo * result = info.createRtlTypeInfo(callback);
  806. if (baseType == type_record)
  807. patchIfBlockParentRow(result, static_cast<const RtlRecordTypeInfo *>(result));
  808. return result;
  809. }
  810. void patchIndexFilePos()
  811. {
  812. if (callback && (base->fieldType & RFTMkind) == type_record)
  813. {
  814. // Yukky hack time
  815. // Assumes that the fieldinfo is not shared...
  816. // But that is also assumed by the code that cleans them up.
  817. const RtlFieldInfo * const *fields = base->queryFields();
  818. for(;;)
  819. {
  820. const RtlFieldInfo *field = *fields++;
  821. if (!field)
  822. break;
  823. if (field->type->getType() == type_blob)
  824. {
  825. static_cast<RtlBlobTypeInfo *>(const_cast<RtlTypeInfo *>(field->type))->setCallback(callback);
  826. }
  827. }
  828. }
  829. }
  830. void patchIfBlockParentRow(const RtlTypeInfo * fieldType, const RtlRecordTypeInfo * parentRow)
  831. {
  832. const RtlFieldInfo * const * fields = fieldType->queryFields();
  833. for (;*fields;fields++)
  834. {
  835. const RtlFieldInfo * cur = *fields;
  836. if (!cur)
  837. break;
  838. const RtlTypeInfo * curType = cur->type;
  839. if ((curType->fieldType & RFTMkind) == type_ifblock)
  840. {
  841. const RtlDynamicIfBlockTypeInfo * constifblock = static_cast<const RtlDynamicIfBlockTypeInfo *>(curType);
  842. RtlDynamicIfBlockTypeInfo * ifblock = const_cast<RtlDynamicIfBlockTypeInfo *>(constifblock);
  843. ifblock->setParent(parentRow);
  844. patchIfBlockParentRow(curType, parentRow);
  845. }
  846. }
  847. }
  848. };
  849. extern ECLRTL_API IRtlFieldTypeDeserializer *createRtlFieldTypeDeserializer(IThorIndexCallback *callback)
  850. {
  851. return new CRtlFieldTypeDeserializer(callback);
  852. }
  853. extern ECLRTL_API StringBuffer &dumpTypeInfo(StringBuffer &ret, const RtlTypeInfo *t)
  854. {
  855. return CRtlFieldTypeSerializer::serialize(ret, t);
  856. }
  857. extern ECLRTL_API bool dumpTypeInfo(MemoryBuffer &ret, const RtlTypeInfo *t)
  858. {
  859. try
  860. {
  861. CRtlFieldTypeBinSerializer::serialize(ret, t);
  862. return true;
  863. }
  864. catch (IException *E)
  865. {
  866. EXCLOG(E);
  867. return false;
  868. }
  869. }
  870. extern ECLRTL_API void serializeRecordType(size32_t & __lenResult, void * & __result, IOutputMetaData & metaVal)
  871. {
  872. MemoryBuffer ret;
  873. try
  874. {
  875. CRtlFieldTypeBinSerializer::serialize(ret, metaVal.queryTypeInfo());
  876. }
  877. catch (IException * e)
  878. {
  879. ret.clear();
  880. e->Release();
  881. }
  882. __lenResult = ret.length();
  883. __result = ret.detach();
  884. }
  885. extern ECLRTL_API void dumpRecordType(size32_t & __lenResult,char * & __result,IOutputMetaData &metaVal)
  886. {
  887. StringBuffer ret;
  888. try
  889. {
  890. CRtlFieldTypeSerializer::serialize(ret, metaVal.queryTypeInfo());
  891. #ifdef _DEBUG
  892. StringBuffer ret2;
  893. CRtlFieldTypeDeserializer deserializer(nullptr);
  894. CRtlFieldTypeSerializer::serialize(ret2, deserializer.deserialize(ret));
  895. assert(streq(ret, ret2));
  896. MemoryBuffer out;
  897. CRtlFieldTypeBinSerializer::serialize(out, metaVal.queryTypeInfo());
  898. CRtlFieldTypeDeserializer bindeserializer(nullptr);
  899. CRtlFieldTypeSerializer::serialize(ret2.clear(), bindeserializer.deserialize(out));
  900. assert(streq(ret, ret2));
  901. #endif
  902. }
  903. catch (IException * e)
  904. {
  905. e->errorMessage(ret.clear());
  906. e->Release();
  907. }
  908. __lenResult = ret.length();
  909. __result = ret.detach();
  910. }
  911. extern ECLRTL_API void getFieldVal(size32_t & __lenResult,char * & __result, int column, IOutputMetaData & metaVal, const byte *row)
  912. {
  913. __lenResult = 0;
  914. __result = nullptr;
  915. if (column >= 0)
  916. {
  917. const RtlRecord &r = metaVal.queryRecordAccessor(true);
  918. if (column < r.getNumFields())
  919. {
  920. unsigned numOffsets = r.getNumVarFields() + 1;
  921. size_t * variableOffsets = (size_t *)alloca(numOffsets * sizeof(size_t));
  922. RtlRow offsetCalculator(r, row, numOffsets, variableOffsets);
  923. offsetCalculator.getUtf8(__lenResult, __result, column);
  924. }
  925. }
  926. }
  927. extern ECLRTL_API int getFieldNum(const char *fieldName, IOutputMetaData & metaVal)
  928. {
  929. const RtlRecord r = metaVal.queryRecordAccessor(true);
  930. return r.getFieldNum(fieldName);
  931. }
  932. enum FieldMatchType {
  933. // On a field, exactly one of the below is set, but translator returns a bitmap indicating
  934. // which were required (and we can restrict translation to allow some types but not others)
  935. match_perfect = 0x00, // exact type match - use memcpy
  936. match_link = 0x01, // copy a nested dataset by linking
  937. match_move = 0x02, // at least one field has moved (set on translator)
  938. match_remove = 0x04, // at least one field has been removed (set on translator)
  939. match_truncate = 0x08, // dest is truncated copy of source - use memcpy
  940. match_extend = 0x10, // dest is padded version of source - use memcpy and memset
  941. match_typecast = 0x20, // type has changed - cast required
  942. match_none = 0x40, // No matching field in source - use null value
  943. match_recurse = 0x80, // Use recursive translator for child records/datasets
  944. match_fail = 0x100, // no translation possible
  945. match_keychange = 0x200, // at least one affected field not marked as payload (set on translator)
  946. // This flag may be set in conjunction with the others
  947. match_inifblock = 0x400, // matching to a field in an ifblock - may not be present
  948. };
  949. StringBuffer &describeFlags(StringBuffer &out, FieldMatchType flags)
  950. {
  951. if (flags == match_perfect)
  952. return out.append("perfect");
  953. unsigned origlen = out.length();
  954. if (flags & match_link) out.append("|link");
  955. if (flags & match_move) out.append("|move");
  956. if (flags & match_remove) out.append("|remove");
  957. if (flags & match_truncate) out.append("|truncate");
  958. if (flags & match_extend) out.append("|extend");
  959. if (flags & match_typecast) out.append("|typecast");
  960. if (flags & match_none) out.append("|none");
  961. if (flags & match_recurse) out.append("|recurse");
  962. if (flags & match_inifblock) out.append("|ifblock");
  963. if (flags & match_keychange) out.append("|keychange");
  964. if (flags & match_fail) out.append("|fail");
  965. assertex(out.length() > origlen);
  966. return out.remove(origlen, 1);
  967. }
  968. inline constexpr FieldMatchType operator|(FieldMatchType a, FieldMatchType b) { return (FieldMatchType)((int)a | (int)b); }
  969. inline FieldMatchType &operator|=(FieldMatchType &a, FieldMatchType b) { return (FieldMatchType &) ((int &)a |= (int)b); }
  970. class GeneralRecordTranslator : public CInterfaceOf<IDynamicTransform>
  971. {
  972. public:
  973. GeneralRecordTranslator(const RtlRecord &_destRecInfo, const RtlRecord &_srcRecInfo)
  974. : destRecInfo(_destRecInfo), sourceRecInfo(_srcRecInfo)
  975. {
  976. matchInfo = new MatchInfo[destRecInfo.getNumFields()];
  977. createMatchInfo();
  978. }
  979. ~GeneralRecordTranslator()
  980. {
  981. delete [] matchInfo;
  982. }
  983. virtual void describe() const override
  984. {
  985. doDescribe(0);
  986. }
  987. virtual size32_t translate(ARowBuilder &builder, const byte *sourceRec) const override
  988. {
  989. return doTranslate(builder, 0, sourceRec);
  990. }
  991. virtual size32_t translate(ARowBuilder &builder, const RtlRow &sourceRow) const override
  992. {
  993. sourceRow.lazyCalcOffsets(-1); // MORE - could save the max one we actually need...
  994. return doTranslate(builder, 0, sourceRow);
  995. }
  996. virtual bool canTranslate() const override
  997. {
  998. return (matchFlags & match_fail) == 0;
  999. }
  1000. virtual bool needsTranslate() const override
  1001. {
  1002. return (matchFlags & ~match_link) != 0;
  1003. }
  1004. virtual bool keyedTranslated() const override
  1005. {
  1006. return (matchFlags & match_keychange) != 0;
  1007. }
  1008. private:
  1009. void doDescribe(unsigned indent) const
  1010. {
  1011. for (unsigned idx = 0; idx < destRecInfo.getNumFields(); idx++)
  1012. {
  1013. const char *source = destRecInfo.queryName(idx);
  1014. const MatchInfo &match = matchInfo[idx];
  1015. if (match.matchType == match_none)
  1016. DBGLOG("%*sNo match for field %s - default value will be used", indent, "", source);
  1017. else
  1018. {
  1019. StringBuffer matchStr;
  1020. DBGLOG("%*sMatch (%s) to field %d for field %s (%x)", indent, "", describeFlags(matchStr, match.matchType).str(), match.matchIdx, source, destRecInfo.queryType(idx)->fieldType);
  1021. if (match.subTrans)
  1022. match.subTrans->doDescribe(indent+2);
  1023. }
  1024. }
  1025. if (!canTranslate())
  1026. DBGLOG("%*sTranslation is NOT possible", indent, "");
  1027. else if (needsTranslate())
  1028. {
  1029. StringBuffer matchStr;
  1030. DBGLOG("%*sTranslation is possible (%s)", indent, "", describeFlags(matchStr, matchFlags).str());
  1031. }
  1032. else
  1033. DBGLOG("%*sTranslation is not necessary", indent, "");
  1034. }
  1035. size32_t doTranslate(ARowBuilder &builder, size32_t offset, const byte *sourceRec) const
  1036. {
  1037. unsigned numOffsets = sourceRecInfo.getNumVarFields() + 1;
  1038. size_t * variableOffsets = (size_t *)alloca(numOffsets * sizeof(size_t));
  1039. RtlRow sourceRow(sourceRecInfo, sourceRec, numOffsets, variableOffsets); // MORE - could save the max source offset we actually need, and only set up that many...
  1040. return doTranslate(builder, offset, sourceRow);
  1041. }
  1042. size32_t doTranslate(ARowBuilder &builder, size32_t offset, const RtlRow &sourceRow) const
  1043. {
  1044. dbgassertex(canTranslate());
  1045. byte * destConditions = (byte *)alloca(destRecInfo.getNumIfBlocks() * sizeof(byte));
  1046. memset(destConditions, 2, destRecInfo.getNumIfBlocks() * sizeof(byte));
  1047. size32_t estimate = destRecInfo.getFixedSize();
  1048. if (!estimate)
  1049. {
  1050. estimate = estimateNewSize(sourceRow);
  1051. builder.ensureCapacity(offset+estimate, "record");
  1052. }
  1053. size32_t origOffset = offset;
  1054. for (unsigned idx = 0; idx < destRecInfo.getNumFields(); idx++)
  1055. {
  1056. const RtlFieldInfo *field = destRecInfo.queryField(idx);
  1057. if (field->omitable() && destRecInfo.excluded(field, builder.getSelf(), destConditions))
  1058. continue;
  1059. const RtlTypeInfo *type = field->type;
  1060. const MatchInfo &match = matchInfo[idx];
  1061. if (match.matchType == match_none || match.matchType==match_fail)
  1062. offset = type->buildNull(builder, offset, field);
  1063. else
  1064. {
  1065. unsigned matchField = match.matchIdx;
  1066. const RtlTypeInfo *sourceType = sourceRecInfo.queryType(matchField);
  1067. size_t sourceOffset = sourceRow.getOffset(matchField);
  1068. const byte *source = sourceRow.queryRow() + sourceOffset;
  1069. size_t copySize = sourceRow.getSize(matchField);
  1070. if (copySize == 0 && (match.matchType & match_inifblock)) // Field is missing because of an ifblock - use default value
  1071. {
  1072. offset = type->buildNull(builder, offset, field);
  1073. }
  1074. else
  1075. {
  1076. switch (match.matchType & ~match_inifblock)
  1077. {
  1078. case match_perfect:
  1079. {
  1080. // Look ahead for other perfect matches and combine the copies
  1081. while (idx < destRecInfo.getNumFields()-1)
  1082. {
  1083. const MatchInfo &nextMatch = matchInfo[idx+1];
  1084. if (nextMatch.matchType == match_perfect && nextMatch.matchIdx == matchField+1)
  1085. {
  1086. idx++;
  1087. matchField++;
  1088. }
  1089. else
  1090. break;
  1091. }
  1092. size_t copySize = sourceRow.getOffset(matchField+1) - sourceOffset;
  1093. builder.ensureCapacity(offset+copySize, field->name);
  1094. memcpy(builder.getSelf()+offset, source, copySize);
  1095. offset += copySize;
  1096. break;
  1097. }
  1098. case match_truncate:
  1099. {
  1100. assert(type->isFixedSize());
  1101. size32_t copySize = type->getMinSize();
  1102. builder.ensureCapacity(offset+copySize, field->name);
  1103. memcpy(builder.getSelf()+offset, source, copySize);
  1104. offset += copySize;
  1105. break;
  1106. }
  1107. case match_extend:
  1108. {
  1109. assert(type->isFixedSize());
  1110. size32_t destSize = type->getMinSize();
  1111. builder.ensureCapacity(offset+destSize, field->name);
  1112. memcpy(builder.getSelf()+offset, source, copySize);
  1113. offset += copySize;
  1114. unsigned fillSize = destSize - copySize;
  1115. memset(builder.getSelf()+offset, match.fillChar, fillSize);
  1116. offset += fillSize;
  1117. break;
  1118. }
  1119. case match_typecast:
  1120. offset = translateScalar(builder, offset, field, type, sourceType, source);
  1121. break;
  1122. case match_link:
  1123. {
  1124. // a 32-bit record count, and a (linked) pointer to an array of record pointers
  1125. byte *dest = builder.ensureCapacity(offset+sizeof(size32_t)+sizeof(const byte **), field->name)+offset;
  1126. *(size32_t *)dest = *(size32_t *)source;
  1127. *(const byte ***)(dest + sizeof(size32_t)) = rtlLinkRowset(*(const byte ***)(source + sizeof(size32_t)));
  1128. offset += sizeof(size32_t)+sizeof(const byte **);
  1129. break;
  1130. }
  1131. case match_recurse:
  1132. if (type->getType()==type_record)
  1133. offset = match.subTrans->doTranslate(builder, offset, source);
  1134. else if (type->isLinkCounted())
  1135. {
  1136. // a 32-bit record count, and a pointer to an array of record pointers
  1137. IEngineRowAllocator *childAllocator = builder.queryAllocator()->createChildRowAllocator(type->queryChildType());
  1138. assertex(childAllocator); // May not be available when using serialized types (but unlikely to want to create linkcounted children remotely either)
  1139. size32_t sizeInBytes = sizeof(size32_t) + sizeof(void *);
  1140. builder.ensureCapacity(offset+sizeInBytes, field->name);
  1141. size32_t numRows = 0;
  1142. const byte **childRows = nullptr;
  1143. if (sourceType->isLinkCounted())
  1144. {
  1145. // a 32-bit count, then a pointer to the source rows
  1146. size32_t childCount = *(size32_t *) source;
  1147. source += sizeof(size32_t);
  1148. const byte ** sourceRows = *(const byte***) source;
  1149. for (size32_t childRow = 0; childRow < childCount; childRow++)
  1150. {
  1151. RtlDynamicRowBuilder childBuilder(*childAllocator);
  1152. size32_t childLen = match.subTrans->doTranslate(childBuilder, 0, sourceRows[childRow]);
  1153. childRows = childAllocator->appendRowOwn(childRows, ++numRows, (void *) childBuilder.finalizeRowClear(childLen));
  1154. }
  1155. }
  1156. else
  1157. {
  1158. // a 32-bit size, then rows inline
  1159. size32_t childSize = *(size32_t *) source;
  1160. source += sizeof(size32_t);
  1161. const byte *initialSource = source;
  1162. while ((size_t)(source - initialSource) < childSize)
  1163. {
  1164. RtlDynamicRowBuilder childBuilder(*childAllocator);
  1165. size32_t childLen = match.subTrans->doTranslate(childBuilder, 0, source);
  1166. childRows = childAllocator->appendRowOwn(childRows, ++numRows, (void *) childBuilder.finalizeRowClear(childLen));
  1167. source += sourceType->queryChildType()->size(source, nullptr); // MORE - shame to repeat a calculation that the translate above almost certainly just did
  1168. }
  1169. }
  1170. // Go back in and patch the count, remembering it may have moved
  1171. rtlWriteInt4(builder.getSelf()+offset, numRows);
  1172. * ( const void * * ) (builder.getSelf()+offset+sizeof(size32_t)) = childRows;
  1173. offset += sizeInBytes;
  1174. }
  1175. else
  1176. {
  1177. size32_t countOffset = offset;
  1178. byte *dest = builder.ensureCapacity(offset+sizeof(size32_t), field->name)+offset;
  1179. offset += sizeof(size32_t);
  1180. size32_t initialOffset = offset;
  1181. *(size32_t *)dest = 0; // patched below when true figure known
  1182. if (sourceType->isLinkCounted())
  1183. {
  1184. // a 32-bit count, then a pointer to the source rows
  1185. size32_t childCount = *(size32_t *) source;
  1186. source += sizeof(size32_t);
  1187. const byte ** sourceRows = *(const byte***) source;
  1188. for (size32_t childRow = 0; childRow < childCount; childRow++)
  1189. {
  1190. offset = match.subTrans->doTranslate(builder, offset, sourceRows[childRow]);
  1191. }
  1192. }
  1193. else
  1194. {
  1195. // a 32-bit size, then rows inline
  1196. size32_t childSize = *(size32_t *) source;
  1197. source += sizeof(size32_t);
  1198. const byte *initialSource = source;
  1199. while ((size_t)(source - initialSource) < childSize)
  1200. {
  1201. offset = match.subTrans->doTranslate(builder, offset, source);
  1202. source += sourceType->queryChildType()->size(source, nullptr); // MORE - shame to repeat a calculation that the translate above almost certainly just did
  1203. }
  1204. }
  1205. dest = builder.getSelf() + countOffset; // Note - may have been moved by reallocs since last calculated
  1206. *(size32_t *)dest = offset - initialOffset;
  1207. }
  1208. break;
  1209. default:
  1210. throwUnexpected();
  1211. }
  1212. }
  1213. }
  1214. }
  1215. if (estimate && offset-origOffset != estimate)
  1216. {
  1217. // Note - ifblocks make this assertion invalid. We do not account for potentially omitted fields
  1218. // when estimating target record size.
  1219. if (!destRecInfo.getNumIfBlocks())
  1220. assert(offset-origOffset > estimate); // Estimate is always supposed to be conservative
  1221. #ifdef TRACE_TRANSLATION
  1222. DBGLOG("Wrote %u bytes to record (estimate was %u)\n", offset-origOffset, estimate);
  1223. #endif
  1224. }
  1225. return offset;
  1226. }
  1227. inline FieldMatchType match() const
  1228. {
  1229. return matchFlags;
  1230. }
  1231. const RtlRecord &destRecInfo;
  1232. const RtlRecord &sourceRecInfo;
  1233. unsigned fixedDelta = 0; // total size of all fixed-size source fields that are not matched
  1234. UnsignedArray unmatched; // List of all variable-size source fields that are unmatched
  1235. FieldMatchType matchFlags = match_perfect;
  1236. struct MatchInfo
  1237. {
  1238. unsigned matchIdx = 0;
  1239. FieldMatchType matchType = match_fail;
  1240. char fillChar = 0;
  1241. GeneralRecordTranslator *subTrans = nullptr;
  1242. ~MatchInfo()
  1243. {
  1244. delete subTrans;
  1245. }
  1246. } *matchInfo;
  1247. static size32_t translateScalar(ARowBuilder &builder, size32_t offset, const RtlFieldInfo *field, const RtlTypeInfo *destType, const RtlTypeInfo *sourceType, const byte *source)
  1248. {
  1249. // This code COULD move into rtlfield.cpp?
  1250. switch(destType->getType())
  1251. {
  1252. case type_boolean:
  1253. case type_int:
  1254. case type_swapint:
  1255. case type_packedint:
  1256. case type_filepos:
  1257. offset = destType->buildInt(builder, offset, field, sourceType->getInt(source));
  1258. break;
  1259. case type_real:
  1260. offset = destType->buildReal(builder, offset, field, sourceType->getReal(source));
  1261. break;
  1262. case type_decimal: // Go via string - not common enough to special-case
  1263. case type_data:
  1264. case type_string:
  1265. case type_varstring:
  1266. case type_qstring:
  1267. {
  1268. size32_t size;
  1269. rtlDataAttr text;
  1270. sourceType->getString(size, text.refstr(), source);
  1271. offset = destType->buildString(builder, offset, field, size, text.getstr());
  1272. break;
  1273. }
  1274. case type_unicode:
  1275. case type_varunicode:
  1276. case type_utf8:
  1277. {
  1278. size32_t utf8chars;
  1279. rtlDataAttr utf8Text;
  1280. sourceType->getUtf8(utf8chars, utf8Text.refstr(), source);
  1281. offset = destType->buildUtf8(builder, offset, field, utf8chars, utf8Text.getstr());
  1282. break;
  1283. }
  1284. case type_set:
  1285. {
  1286. bool isAll = *(bool *) source;
  1287. source+= sizeof(bool);
  1288. byte *dest = builder.ensureCapacity(offset+sizeof(bool)+sizeof(size32_t), field->name)+offset;
  1289. *(size32_t *) (dest + sizeof(bool)) = 0; // Patch later when size known
  1290. offset += sizeof(bool) + sizeof(size32_t);
  1291. if (isAll)
  1292. {
  1293. *(bool*) dest = true;
  1294. }
  1295. else
  1296. {
  1297. *(bool*) dest = false;
  1298. size32_t sizeOffset = offset - sizeof(size32_t); // Where we need to patch
  1299. size32_t childSize = *(size32_t *)source;
  1300. source += sizeof(size32_t);
  1301. const byte *initialSource = source;
  1302. size32_t initialOffset = offset;
  1303. const RtlTypeInfo *destChildType = destType->queryChildType();
  1304. const RtlTypeInfo *sourceChildType = sourceType->queryChildType();
  1305. while ((size_t)(source - initialSource) < childSize)
  1306. {
  1307. offset = translateScalar(builder, offset, field, destChildType, sourceChildType, source);
  1308. source += sourceChildType->size(source, nullptr); // MORE - shame to repeat a calculation that the translate above almost certainly just did
  1309. }
  1310. dest = builder.getSelf() + sizeOffset; // Note - man have been moved by reallocs since last calculated
  1311. *(size32_t *)dest = offset - initialOffset;
  1312. }
  1313. break;
  1314. }
  1315. default:
  1316. throwUnexpected();
  1317. }
  1318. return offset;
  1319. }
  1320. size32_t estimateNewSize(const RtlRow &sourceRow) const
  1321. {
  1322. //DBGLOG("Source record size is %d", (int) sourceRow.getRecordSize());
  1323. size32_t expectedSize = sourceRow.getRecordSize() - fixedDelta;
  1324. //DBGLOG("Source record size without omitted fixed size fields is %d", expectedSize);
  1325. ForEachItemIn(i, unmatched)
  1326. {
  1327. unsigned fieldNo = unmatched.item(i);
  1328. expectedSize -= sourceRow.getSize(fieldNo);
  1329. //DBGLOG("Reducing estimated size by %d to %d for omitted field %d (%s)", (int) sourceRow.getSize(fieldNo), expectedSize, fieldNo, sourceRecInfo.queryName(fieldNo));
  1330. }
  1331. if (matchFlags & ~(match_perfect|match_link|match_none|match_extend|match_truncate))
  1332. {
  1333. for (unsigned idx = 0; idx < destRecInfo.getNumFields(); idx++)
  1334. {
  1335. const MatchInfo &match = matchInfo[idx];
  1336. const RtlTypeInfo *type = destRecInfo.queryType(idx);
  1337. unsigned matchField = match.matchIdx;
  1338. switch (match.matchType)
  1339. {
  1340. case match_perfect:
  1341. case match_link:
  1342. case match_none:
  1343. case match_extend:
  1344. case match_truncate:
  1345. // These ones were already included in fixedDelta
  1346. break;
  1347. default:
  1348. // This errs on the side of small - i.e. it assumes that all typecasts end up at minimum size
  1349. // We could do better in some cases e.g. variable string <-> variable unicode we can assume factor of 2,
  1350. // uft8 <-> string we could calculate here - but unlikely to be worth the effort.
  1351. // But it's fine for fixed size output fields, including truncate/extend
  1352. // We could also precalculate the expected delta if all omitted fields are fixed size - but not sure how likely/worthwhile that is.
  1353. expectedSize += type->getMinSize() - sourceRow.getSize(matchField);
  1354. //DBGLOG("Adjusting estimated size by (%d - %d) to %d for translated field %d (%s)", (int) sourceRow.getSize(matchField), type->getMinSize(), expectedSize, matchField, sourceRecInfo.queryName(matchField));
  1355. break;
  1356. }
  1357. }
  1358. }
  1359. return expectedSize;
  1360. }
  1361. void createMatchInfo()
  1362. {
  1363. for (unsigned idx = 0; idx < destRecInfo.getNumFields(); idx++)
  1364. {
  1365. const RtlFieldInfo *field = destRecInfo.queryField(idx);
  1366. const RtlTypeInfo *type = field->type;
  1367. MatchInfo &info = matchInfo[idx];
  1368. info.matchIdx = sourceRecInfo.getFieldNum(destRecInfo.queryName(idx));
  1369. if (info.matchIdx == -1)
  1370. {
  1371. info.matchType = match_none;
  1372. size32_t defaultSize = field->initializer ? type->size(field->initializer, nullptr) : type->getMinSize();
  1373. fixedDelta -= defaultSize;
  1374. if ((field->flags & RFTMispayloadfield) == 0)
  1375. matchFlags |= match_keychange;
  1376. //DBGLOG("Decreasing fixedDelta size by %d to %d for defaulted field %d (%s)", defaultSize, fixedDelta, idx, destRecInfo.queryName(idx));
  1377. }
  1378. else
  1379. {
  1380. const RtlTypeInfo *sourceType = sourceRecInfo.queryType(info.matchIdx);
  1381. if (!type->isScalar() || !sourceType->isScalar())
  1382. {
  1383. if (type->getType() != sourceType->getType())
  1384. info.matchType = match_fail; // No translation from one non-scalar type to another
  1385. else
  1386. {
  1387. switch (type->getType())
  1388. {
  1389. case type_set:
  1390. if (type->queryChildType()->fieldType==sourceType->queryChildType()->fieldType &&
  1391. type->queryChildType()->length==sourceType->queryChildType()->length)
  1392. info.matchType = match_perfect;
  1393. else
  1394. info.matchType = match_typecast;
  1395. break;
  1396. case type_row: // These are not expected I think...
  1397. throwUnexpected();
  1398. case type_ifblock:
  1399. case type_record:
  1400. case type_table:
  1401. {
  1402. const RtlRecord *subDest = destRecInfo.queryNested(idx);
  1403. const RtlRecord *subSrc = sourceRecInfo.queryNested(info.matchIdx);
  1404. info.subTrans = new GeneralRecordTranslator(*subDest, *subSrc);
  1405. if (!info.subTrans->needsTranslate())
  1406. {
  1407. // Child does not require translation, but check linkcount mode matches too!
  1408. if (type->isLinkCounted())
  1409. if (sourceType->isLinkCounted())
  1410. info.matchType = match_link;
  1411. else
  1412. info.matchType = match_recurse;
  1413. else
  1414. if (sourceType->isLinkCounted())
  1415. info.matchType = match_recurse;
  1416. else
  1417. info.matchType = match_perfect;
  1418. if (info.matchType != match_recurse)
  1419. {
  1420. delete info.subTrans;
  1421. info.subTrans = nullptr;
  1422. }
  1423. }
  1424. else if (info.subTrans->canTranslate())
  1425. {
  1426. info.matchType = match_recurse;
  1427. matchFlags |= info.subTrans->matchFlags;
  1428. }
  1429. else
  1430. info.matchType = match_fail;
  1431. break;
  1432. }
  1433. default:
  1434. info.matchType = match_fail;
  1435. break;
  1436. }
  1437. }
  1438. }
  1439. else if (type->fieldType==sourceType->fieldType)
  1440. {
  1441. if (type->length==sourceType->length)
  1442. {
  1443. info.matchType = match_perfect;
  1444. }
  1445. else
  1446. {
  1447. assert(type->isFixedSize()); // Both variable size would have matched length above
  1448. info.matchType = match_typecast;
  1449. if (type->length < sourceType->length)
  1450. {
  1451. if (type->canTruncate())
  1452. {
  1453. info.matchType = match_truncate;
  1454. fixedDelta += sourceType->getMinSize()-type->getMinSize();
  1455. //DBGLOG("Increasing fixedDelta size by %d to %d for truncated field %d (%s)", sourceType->getMinSize()-type->getMinSize(), fixedDelta, idx, destRecInfo.queryName(idx));
  1456. }
  1457. }
  1458. else
  1459. {
  1460. if (type->canExtend(info.fillChar))
  1461. {
  1462. info.matchType = match_extend;
  1463. fixedDelta += sourceType->getMinSize()-type->getMinSize();
  1464. //DBGLOG("Decreasing fixedDelta size by %d to %d for truncated field %d (%s)", type->getMinSize()-sourceType->getMinSize(), fixedDelta, idx, destRecInfo.queryName(idx));
  1465. }
  1466. }
  1467. }
  1468. }
  1469. else
  1470. info.matchType = match_typecast;
  1471. unsigned sourceFlags = sourceRecInfo.queryField(info.matchIdx)->flags;
  1472. if (sourceFlags & RFTMinifblock)
  1473. info.matchType |= match_inifblock; // Avoids incorrect commoning up of adjacent matches
  1474. // MORE - could note the highest interesting fieldnumber in the source and not bother filling in offsets after that
  1475. // Not sure it would help much though - usually need to know the total record size anyway in real life
  1476. if (idx != info.matchIdx)
  1477. matchFlags |= match_move;
  1478. if (info.matchType != match_perfect && ((field->flags & RFTMispayloadfield) == 0 || (sourceFlags & RFTMispayloadfield) == 0))
  1479. matchFlags |= match_keychange;
  1480. }
  1481. matchFlags |= info.matchType;
  1482. }
  1483. if (sourceRecInfo.getNumFields() > destRecInfo.getNumFields())
  1484. matchFlags |= match_remove;
  1485. if (matchFlags && !destRecInfo.getFixedSize())
  1486. {
  1487. for (unsigned idx = 0; idx < sourceRecInfo.getNumFields(); idx++)
  1488. {
  1489. const RtlFieldInfo *field = sourceRecInfo.queryField(idx);
  1490. const char *name = sourceRecInfo.queryName(idx);
  1491. if (destRecInfo.getFieldNum(name) == (unsigned) -1)
  1492. {
  1493. // unmatched field
  1494. if ((field->flags & RFTMispayloadfield) == 0)
  1495. matchFlags |= match_keychange;
  1496. if (!destRecInfo.getFixedSize())
  1497. {
  1498. const RtlTypeInfo *type = field->type;
  1499. if (type->isFixedSize())
  1500. {
  1501. //DBGLOG("Reducing estimated size by %d for (fixed size) omitted field %s", (int) type->getMinSize(), field->name);
  1502. fixedDelta += type->getMinSize();
  1503. }
  1504. else
  1505. unmatched.append(idx);
  1506. }
  1507. }
  1508. }
  1509. //DBGLOG("Source record contains %d bytes of omitted fixed size fields", fixedDelta);
  1510. }
  1511. }
  1512. };
  1513. extern ECLRTL_API const IDynamicTransform *createRecordTranslator(const RtlRecord &_destRecInfo, const RtlRecord &_srcRecInfo)
  1514. {
  1515. return new GeneralRecordTranslator(_destRecInfo, _srcRecInfo);
  1516. }
  1517. class TranslatedRowStream : public CInterfaceOf<IRowStream>
  1518. {
  1519. public:
  1520. TranslatedRowStream(IRowStream *_inputStream, IEngineRowAllocator *_resultAllocator, const RtlRecord &outputRecord, const RtlRecord &inputRecord)
  1521. : inputStream(_inputStream), resultAllocator(_resultAllocator), translator(new GeneralRecordTranslator(outputRecord, inputRecord))
  1522. {
  1523. translator->describe();
  1524. }
  1525. virtual const void *nextRow()
  1526. {
  1527. if (eof)
  1528. return NULL;
  1529. const void *inRow = inputStream->nextRow();
  1530. if (!inRow)
  1531. {
  1532. if (eogSeen)
  1533. eof = true;
  1534. else
  1535. eogSeen = true;
  1536. return nullptr;
  1537. }
  1538. else
  1539. eogSeen = false;
  1540. RtlDynamicRowBuilder rowBuilder(resultAllocator);
  1541. size32_t len = translator->translate(rowBuilder, (const byte *) inRow);
  1542. rtlReleaseRow(inRow);
  1543. return rowBuilder.finalizeRowClear(len);
  1544. }
  1545. virtual void stop() override
  1546. {
  1547. resultAllocator.clear();
  1548. }
  1549. bool canTranslate() const
  1550. {
  1551. return translator->canTranslate();
  1552. }
  1553. bool needsTranslate() const
  1554. {
  1555. return translator->needsTranslate();
  1556. }
  1557. protected:
  1558. Linked<IRowStream> inputStream;
  1559. Linked<IEngineRowAllocator> resultAllocator;
  1560. Owned<const IDynamicTransform> translator;
  1561. unsigned numOffsets = 0;
  1562. size_t * variableOffsets = nullptr;
  1563. bool eof = false;
  1564. bool eogSeen = false;
  1565. };
  1566. extern ECLRTL_API IRowStream * transformRecord(IEngineRowAllocator * resultAllocator,IOutputMetaData & metaInput,IRowStream * input)
  1567. {
  1568. if (resultAllocator->queryOutputMeta()==&metaInput)
  1569. return LINK(input);
  1570. Owned<TranslatedRowStream> stream = new TranslatedRowStream(input, resultAllocator,
  1571. resultAllocator->queryOutputMeta()->queryRecordAccessor(true),
  1572. metaInput.queryRecordAccessor(true));
  1573. if (!stream->needsTranslate())
  1574. return LINK(input);
  1575. else if (!stream->canTranslate())
  1576. rtlFail(0, "Cannot translate record stream");
  1577. else
  1578. return stream.getClear();
  1579. }
  1580. // A key translator allows us to transform a RowFilter that refers to src to one that refers to dest.
  1581. // Basically just a map of those fields with matching types.
  1582. class CKeyTranslator : public CInterfaceOf<IKeyTranslator>
  1583. {
  1584. public:
  1585. CKeyTranslator(const RtlRecord &actual, const RtlRecord &expected)
  1586. {
  1587. translateNeeded = false;
  1588. for (unsigned expectedIdx = 0; expectedIdx < expected.getNumFields(); expectedIdx++)
  1589. {
  1590. unsigned actualIdx = actual.getFieldNum(expected.queryName(expectedIdx));
  1591. if (actualIdx != -1)
  1592. {
  1593. const RtlTypeInfo *expectedType = expected.queryType(expectedIdx);
  1594. const RtlTypeInfo *actualType = actual.queryType(actualIdx);
  1595. if (!actualType->equivalent(expectedType))
  1596. actualIdx = (unsigned) -2;
  1597. }
  1598. map.append(actualIdx);
  1599. if (actualIdx != expectedIdx)
  1600. translateNeeded = true;
  1601. }
  1602. }
  1603. virtual void describe() const override
  1604. {
  1605. ForEachItemIn(idx, map)
  1606. {
  1607. unsigned mapped = map.item(idx);
  1608. switch (mapped)
  1609. {
  1610. case (unsigned) -1: DBGLOG("No match for field %d", idx); break;
  1611. case (unsigned) -2: DBGLOG("Incompatible field match for field %d", idx); break;
  1612. default: DBGLOG("keyed field %d can map to field %d", idx, mapped); break;
  1613. }
  1614. }
  1615. }
  1616. virtual bool translate(RowFilter &filters) const override // MORE - remove this version - it's not safe
  1617. {
  1618. bool mapNeeded = false;
  1619. if (translateNeeded)
  1620. {
  1621. unsigned numFields = filters.numFilterFields();
  1622. for (unsigned idx = 0; idx < numFields; idx++)
  1623. {
  1624. unsigned fieldNum = filters.queryFilter(idx).queryFieldIndex();
  1625. unsigned mappedFieldNum = map.isItem(fieldNum) ? map.item(fieldNum) : (unsigned) -1;
  1626. if (mappedFieldNum != fieldNum)
  1627. {
  1628. mapNeeded = true;
  1629. switch (mappedFieldNum)
  1630. {
  1631. case (unsigned) -1: throw makeStringExceptionV(0, "Cannot translate keyed filter on field %u - no matching field", idx);
  1632. case (unsigned) -2: throw makeStringExceptionV(0, "Cannot translate keyed filter on field %u - incompatible matching field type", idx);
  1633. default:
  1634. filters.remapField(idx, mappedFieldNum);
  1635. break;
  1636. }
  1637. }
  1638. }
  1639. if (mapNeeded)
  1640. filters.recalcFieldsRequired();
  1641. }
  1642. return mapNeeded;
  1643. }
  1644. virtual bool translate(RowFilter &filter, IConstArrayOf<IFieldFilter> &in) const override
  1645. {
  1646. bool mapNeeded = false;
  1647. if (translateNeeded)
  1648. {
  1649. unsigned numFields = in.length();
  1650. for (unsigned idx = 0; idx < numFields; idx++)
  1651. {
  1652. unsigned fieldNum = in.item(idx).queryFieldIndex();
  1653. unsigned mappedFieldNum = map.isItem(fieldNum) ? map.item(fieldNum) : (unsigned) -1;
  1654. if (mappedFieldNum != fieldNum)
  1655. {
  1656. mapNeeded = true;
  1657. switch (mappedFieldNum)
  1658. {
  1659. case (unsigned) -1: throw makeStringExceptionV(0, "Cannot translate keyed filter on field %u - no matching field", idx);
  1660. case (unsigned) -2: throw makeStringExceptionV(0, "Cannot translate keyed filter on field %u - incompatible matching field type", idx);
  1661. default:
  1662. filter.addFilter(*in.item(idx).remap(mappedFieldNum));
  1663. break;
  1664. }
  1665. }
  1666. else
  1667. filter.addFilter(OLINK(in.item(idx)));
  1668. }
  1669. }
  1670. return mapNeeded;
  1671. }
  1672. virtual bool needsTranslate() const
  1673. {
  1674. return translateNeeded;
  1675. }
  1676. protected:
  1677. UnsignedArray map;
  1678. bool translateNeeded = false;
  1679. };
  1680. extern ECLRTL_API const IKeyTranslator *createKeyTranslator(const RtlRecord &_destRecInfo, const RtlRecord &_srcRecInfo)
  1681. {
  1682. return new CKeyTranslator(_destRecInfo, _srcRecInfo);
  1683. }