rtlds.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "platform.h"
  15. #include "jliball.hpp"
  16. #include "eclrtl.hpp"
  17. #include "eclhelper.hpp"
  18. #include "rtlds_imp.hpp"
  19. #include "rtlread_imp.hpp"
  20. #define FIRST_CHUNK_SIZE 0x100
  21. #define DOUBLE_LIMIT 0x10000 // must be a power of 2
  22. unsigned getNextSize(unsigned max, unsigned required)
  23. {
  24. if (required > DOUBLE_LIMIT)
  25. {
  26. max = (required + DOUBLE_LIMIT) & ~(DOUBLE_LIMIT-1);
  27. if (required >= max)
  28. throw MakeStringException(-1, "getNextSize: Request for %d bytes oldMax = %d", required, max);
  29. }
  30. else
  31. {
  32. if (max == 0)
  33. max = FIRST_CHUNK_SIZE;
  34. while (required >= max)
  35. max += max;
  36. }
  37. return max;
  38. }
  39. //---------------------------------------------------------------------------
  40. RtlDatasetBuilder::RtlDatasetBuilder()
  41. {
  42. maxSize = 0;
  43. buffer = NULL;
  44. totalSize = 0;
  45. }
  46. RtlDatasetBuilder::~RtlDatasetBuilder()
  47. {
  48. free(buffer);
  49. }
  50. void RtlDatasetBuilder::ensure(size32_t required)
  51. {
  52. if (required > maxSize)
  53. {
  54. maxSize = getNextSize(maxSize, required);
  55. buffer = (byte *)realloc(buffer, maxSize);
  56. if (!buffer)
  57. throw MakeStringException(-1, "Failed to allocate temporary dataset (requesting %d bytes)", maxSize);
  58. }
  59. }
  60. byte * RtlDatasetBuilder::ensureCapacity(size32_t required, const char * fieldName)
  61. {
  62. ensure(totalSize + required);
  63. return buffer + totalSize;
  64. }
  65. void RtlDatasetBuilder::flushDataset()
  66. {
  67. }
  68. void RtlDatasetBuilder::getData(size32_t & len, void * & data)
  69. {
  70. flushDataset();
  71. len = totalSize;
  72. data = malloc(totalSize);
  73. memcpy(data, buffer, totalSize);
  74. }
  75. size32_t RtlDatasetBuilder::getSize()
  76. {
  77. flushDataset();
  78. return totalSize;
  79. }
  80. byte * RtlDatasetBuilder::queryData()
  81. {
  82. flushDataset();
  83. return buffer;
  84. }
  85. void RtlDatasetBuilder::reportMissingRow() const
  86. {
  87. throw MakeStringException(MSGAUD_user, 1000, "RtlDatasetBuilder::row() is NULL");
  88. }
  89. //---------------------------------------------------------------------------
  90. RtlFixedDatasetBuilder::RtlFixedDatasetBuilder(unsigned _recordSize, unsigned maxRows)
  91. {
  92. recordSize = _recordSize;
  93. if ((int)maxRows > 0)
  94. ensure(recordSize * maxRows);
  95. }
  96. byte * RtlFixedDatasetBuilder::createSelf()
  97. {
  98. self = ensureCapacity(recordSize, NULL);
  99. return self;
  100. }
  101. //---------------------------------------------------------------------------
  102. RtlLimitedFixedDatasetBuilder::RtlLimitedFixedDatasetBuilder(unsigned _recordSize, unsigned _maxRows, DefaultRowCreator _rowCreator, IResourceContext *_ctx) : RtlFixedDatasetBuilder(_recordSize, _maxRows)
  103. {
  104. maxRows = _maxRows;
  105. if ((int)maxRows < 0) maxRows = 0;
  106. rowCreator = _rowCreator;
  107. ctx = _ctx;
  108. }
  109. byte * RtlLimitedFixedDatasetBuilder::createRow()
  110. {
  111. if (totalSize >= maxRows * recordSize)
  112. return NULL;
  113. return RtlFixedDatasetBuilder::createRow();
  114. }
  115. void RtlLimitedFixedDatasetBuilder::flushDataset()
  116. {
  117. if (rowCreator)
  118. {
  119. while (totalSize < maxRows * recordSize)
  120. {
  121. createRow();
  122. size32_t size = rowCreator(rowBuilder(), ctx);
  123. finalizeRow(size);
  124. }
  125. }
  126. RtlFixedDatasetBuilder::flushDataset();
  127. }
  128. //---------------------------------------------------------------------------
  129. RtlVariableDatasetBuilder::RtlVariableDatasetBuilder(IRecordSize & _recordSize)
  130. {
  131. recordSize = &_recordSize;
  132. maxRowSize = recordSize->getRecordSize(NULL); // initial size
  133. }
  134. byte * RtlVariableDatasetBuilder::createSelf()
  135. {
  136. self = ensureCapacity(maxRowSize, NULL);
  137. return self;
  138. }
  139. void RtlVariableDatasetBuilder::deserializeRow(IOutputRowDeserializer & deserializer, IRowDeserializerSource & in)
  140. {
  141. createRow();
  142. size32_t rowSize = deserializer.deserialize(rowBuilder(), in);
  143. finalizeRow(rowSize);
  144. }
  145. //---------------------------------------------------------------------------
  146. RtlLimitedVariableDatasetBuilder::RtlLimitedVariableDatasetBuilder(IRecordSize & _recordSize, unsigned _maxRows, DefaultRowCreator _rowCreator, IResourceContext *_ctx) : RtlVariableDatasetBuilder(_recordSize)
  147. {
  148. numRows = 0;
  149. maxRows = _maxRows;
  150. rowCreator = _rowCreator;
  151. ctx = _ctx;
  152. }
  153. byte * RtlLimitedVariableDatasetBuilder::createRow()
  154. {
  155. if (numRows >= maxRows)
  156. return NULL;
  157. numRows++;
  158. return RtlVariableDatasetBuilder::createRow();
  159. }
  160. void RtlLimitedVariableDatasetBuilder::flushDataset()
  161. {
  162. if (rowCreator)
  163. {
  164. while (numRows < maxRows)
  165. {
  166. createRow();
  167. size32_t thisSize = rowCreator(rowBuilder(), ctx);
  168. finalizeRow(thisSize);
  169. }
  170. }
  171. RtlVariableDatasetBuilder::flushDataset();
  172. }
  173. //---------------------------------------------------------------------------
  174. byte * * rtlRowsAttr::linkrows() const
  175. {
  176. if (rows)
  177. rtlLinkRowset(rows);
  178. return rows;
  179. }
  180. void rtlRowsAttr::set(size32_t _count, byte * * _rows)
  181. {
  182. setown(_count, rtlLinkRowset(_rows));
  183. }
  184. void rtlRowsAttr::setRow(IEngineRowAllocator * rowAllocator, const byte * _row)
  185. {
  186. setown(1, rowAllocator->appendRowOwn(NULL, 1, rowAllocator->linkRow(_row)));
  187. }
  188. void rtlRowsAttr::setown(size32_t _count, byte * * _rows)
  189. {
  190. dispose();
  191. count = _count;
  192. rows = _rows;
  193. }
  194. void rtlRowsAttr::dispose()
  195. {
  196. if (rows)
  197. rtlReleaseRowset(count, rows);
  198. }
  199. //---------------------------------------------------------------------------
  200. void rtlReportFieldOverflow(unsigned size, unsigned max, const RtlFieldInfo * field)
  201. {
  202. if (field)
  203. rtlReportFieldOverflow(size, max, field->name->str());
  204. else
  205. rtlReportRowOverflow(size, max);
  206. }
  207. void RtlRowBuilderBase::reportMissingRow() const
  208. {
  209. throw MakeStringException(MSGAUD_user, 1000, "RtlRowBuilderBase::row() is NULL");
  210. }
  211. byte * RtlDynamicRowBuilder::ensureCapacity(size32_t required, const char * fieldName)
  212. {
  213. if (required > maxLength)
  214. {
  215. if (!self)
  216. create();
  217. if (required > maxLength)
  218. {
  219. void * next = rowAllocator->resizeRow(required, self, maxLength);
  220. if (!next)
  221. {
  222. rtlReportFieldOverflow(required, maxLength, fieldName);
  223. return NULL;
  224. }
  225. self = static_cast<byte *>(next);
  226. }
  227. }
  228. return self;
  229. }
  230. void RtlDynamicRowBuilder::swapWith(RtlDynamicRowBuilder & other)
  231. {
  232. size32_t savedMaxLength = maxLength;
  233. void * savedSelf = getUnfinalizedClear();
  234. setown(other.getMaxLength(), other.getUnfinalizedClear());
  235. other.setown(savedMaxLength, savedSelf);
  236. }
  237. //---------------------------------------------------------------------------
  238. byte * RtlStaticRowBuilder::ensureCapacity(size32_t required, const char * fieldName)
  239. {
  240. if (required <= maxLength)
  241. return static_cast<byte *>(self);
  242. rtlReportFieldOverflow(required, maxLength, fieldName);
  243. return NULL;
  244. }
  245. byte * RtlStaticRowBuilder::createSelf()
  246. {
  247. throwUnexpected();
  248. }
  249. //---------------------------------------------------------------------------
  250. RtlLinkedDatasetBuilder::RtlLinkedDatasetBuilder(IEngineRowAllocator * _rowAllocator, int _choosenLimit) : builder(_rowAllocator, false)
  251. {
  252. rowAllocator = LINK(_rowAllocator);
  253. rowset = NULL;
  254. count = 0;
  255. max = 0;
  256. choosenLimit = (unsigned)_choosenLimit;
  257. }
  258. RtlLinkedDatasetBuilder::~RtlLinkedDatasetBuilder()
  259. {
  260. builder.clear();
  261. if (rowset)
  262. rowAllocator->releaseRowset(count, rowset);
  263. ::Release(rowAllocator);
  264. }
  265. void RtlLinkedDatasetBuilder::append(const void * source)
  266. {
  267. flush();
  268. if (count < choosenLimit)
  269. {
  270. ensure(count+1);
  271. rowset[count] = (byte *)rowAllocator->linkRow(source);
  272. count++;
  273. }
  274. }
  275. void RtlLinkedDatasetBuilder::appendRows(size32_t num, byte * * rows)
  276. {
  277. flush();
  278. if (num && (count < choosenLimit))
  279. {
  280. unsigned numToAdd = (count + num < choosenLimit) ? num : choosenLimit - count;
  281. ensure(count+numToAdd);
  282. for (unsigned i=0; i < numToAdd; i++)
  283. rowset[count+i] = (byte *)rowAllocator->linkRow(rows[i]);
  284. count += numToAdd;
  285. }
  286. }
  287. void RtlLinkedDatasetBuilder::appendOwn(const void * row)
  288. {
  289. //flush() must have been called before this... otherwise the order will be messed up
  290. assertex(!builder.exists());
  291. if (count < choosenLimit)
  292. {
  293. ensure(count+1);
  294. rowset[count] = (byte *)row;
  295. count++;
  296. }
  297. else
  298. rowAllocator->releaseRow(row);
  299. }
  300. byte * RtlLinkedDatasetBuilder::createRow()
  301. {
  302. flush();
  303. if (count >= choosenLimit)
  304. return NULL;
  305. return builder.getSelf();
  306. }
  307. //cloned from thorcommon.cpp
  308. class RtlChildRowLinkerWalker : implements IIndirectMemberVisitor
  309. {
  310. public:
  311. virtual void visitRowset(size32_t count, byte * * rows)
  312. {
  313. rtlLinkRowset(rows);
  314. }
  315. virtual void visitRow(const byte * row)
  316. {
  317. rtlLinkRow(row);
  318. }
  319. };
  320. void RtlLinkedDatasetBuilder::cloneRow(size32_t len, const void * row)
  321. {
  322. flush();
  323. if (count >= choosenLimit)
  324. return;
  325. byte * self = builder.ensureCapacity(len, NULL);
  326. memcpy(self, row, len);
  327. IOutputMetaData * meta = rowAllocator->queryOutputMeta();
  328. if (meta->getMetaFlags() & MDFneedserialize)
  329. {
  330. RtlChildRowLinkerWalker walker;
  331. meta->walkIndirectMembers(self, walker);
  332. }
  333. finalizeRow(len);
  334. }
  335. void RtlLinkedDatasetBuilder::deserializeRow(IOutputRowDeserializer & deserializer, IRowDeserializerSource & in)
  336. {
  337. flush();
  338. builder.ensureRow();
  339. size32_t rowSize = deserializer.deserialize(builder, in);
  340. finalizeRow(rowSize);
  341. }
  342. inline void doDeserializeRowset(RtlLinkedDatasetBuilder & builder, IOutputRowDeserializer & deserializer, IRowDeserializerSource & in, offset_t marker, bool isGrouped)
  343. {
  344. byte eogPending = false;
  345. while (!in.finishedNested(marker))
  346. {
  347. if (isGrouped && eogPending)
  348. builder.appendEOG();
  349. builder.deserializeRow(deserializer, in);
  350. if (isGrouped)
  351. in.read(1, &eogPending);
  352. }
  353. }
  354. inline void doSerializeRowset(IRowSerializerTarget & out, IOutputRowSerializer * serializer, size32_t count, byte * * rows, bool isGrouped)
  355. {
  356. for (unsigned i=0; i < count; i++)
  357. {
  358. serializer->serialize(out, rows[i]);
  359. if (isGrouped)
  360. {
  361. byte eogPending = (i+1 < count) && (rows[i+1] == NULL);
  362. out.put(1, &eogPending);
  363. }
  364. }
  365. }
  366. void RtlLinkedDatasetBuilder::deserialize(IOutputRowDeserializer & deserializer, IRowDeserializerSource & in, bool isGrouped)
  367. {
  368. flush();
  369. offset_t marker = in.beginNested();
  370. doDeserializeRowset(*this, deserializer, in, marker, isGrouped);
  371. }
  372. void RtlLinkedDatasetBuilder::finalizeRows()
  373. {
  374. flush();
  375. if (count != max)
  376. resize(count);
  377. }
  378. void RtlLinkedDatasetBuilder::flush()
  379. {
  380. // builder.clear();
  381. }
  382. void RtlLinkedDatasetBuilder::finalizeRow(size32_t rowSize)
  383. {
  384. assertex(builder.exists());
  385. const void * next = builder.finalizeRowClear(rowSize);
  386. appendOwn(next);
  387. }
  388. byte * * RtlLinkedDatasetBuilder::linkrows()
  389. {
  390. finalizeRows();
  391. return rtlLinkRowset(rowset);
  392. }
  393. void RtlLinkedDatasetBuilder::expand(size32_t required)
  394. {
  395. assertex(required < choosenLimit);
  396. //MORE: Next factoring change this so it passes this logic over to the row allocator
  397. size32_t newMax = max ? max : 4;
  398. while (newMax < required)
  399. {
  400. newMax += newMax;
  401. assertex(newMax);
  402. }
  403. if (newMax > choosenLimit)
  404. newMax = choosenLimit;
  405. resize(newMax);
  406. }
  407. void RtlLinkedDatasetBuilder::resize(size32_t required)
  408. {
  409. rowset = rowAllocator->reallocRows(rowset, max, required);
  410. max = required;
  411. }
  412. void appendRowsToRowset(size32_t & targetCount, byte * * & targetRowset, IEngineRowAllocator * rowAllocator, size32_t extraCount, byte * * extraRows)
  413. {
  414. if (extraCount)
  415. {
  416. size32_t prevCount = targetCount;
  417. byte * * expandedRowset = rowAllocator->reallocRows(targetRowset, prevCount, prevCount+extraCount);
  418. for (unsigned i=0; i < extraCount; i++)
  419. expandedRowset[prevCount+i] = (byte *)rowAllocator->linkRow(extraRows[i]);
  420. targetCount = prevCount + extraCount;
  421. targetRowset = expandedRowset;
  422. }
  423. }
  424. inline void doDeserializeRowset(size32_t & count, byte * * & rowset, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, IRowDeserializerSource & in, bool isGrouped)
  425. {
  426. RtlLinkedDatasetBuilder builder(_rowAllocator);
  427. builder.deserialize(*deserializer, in, isGrouped);
  428. count = builder.getcount();
  429. rowset = builder.linkrows();
  430. }
  431. extern ECLRTL_API void rtlDeserializeRowset(size32_t & count, byte * * & rowset, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, IRowDeserializerSource & in)
  432. {
  433. doDeserializeRowset(count, rowset, _rowAllocator, deserializer, in, false);
  434. }
  435. extern ECLRTL_API void rtlDeserializeGroupedRowset(size32_t & count, byte * * & rowset, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, IRowDeserializerSource & in)
  436. {
  437. doDeserializeRowset(count, rowset, _rowAllocator, deserializer, in, true);
  438. }
  439. void rtlSerializeRowset(IRowSerializerTarget & out, IOutputRowSerializer * serializer, size32_t count, byte * * rows)
  440. {
  441. size32_t marker = out.beginNested();
  442. doSerializeRowset(out, serializer, count, rows, false);
  443. out.endNested(marker);
  444. }
  445. void rtlSerializeGroupedRowset(IRowSerializerTarget & out, IOutputRowSerializer * serializer, size32_t count, byte * * rows)
  446. {
  447. size32_t marker = out.beginNested();
  448. doSerializeRowset(out, serializer, count, rows, true);
  449. out.endNested(marker);
  450. }
  451. //---------------------------------------------------------------------------
  452. //These definitions should be shared with thorcommon, but to do that
  453. //they would need to be moved to an rtlds.ipp header, which thorcommon then included.
  454. class ECLRTL_API CThorRtlRowSerializer : implements IRowSerializerTarget
  455. {
  456. public:
  457. CThorRtlRowSerializer(MemoryBuffer & _buffer) : buffer(_buffer)
  458. {
  459. }
  460. virtual void put(size32_t len, const void * ptr)
  461. {
  462. buffer.append(len, ptr);
  463. }
  464. virtual size32_t beginNested()
  465. {
  466. unsigned pos = buffer.length();
  467. buffer.append((size32_t)0);
  468. return pos;
  469. }
  470. virtual void endNested(size32_t sizePos)
  471. {
  472. unsigned pos = buffer.length();
  473. buffer.rewrite(sizePos);
  474. buffer.append((size32_t)(pos - (sizePos + sizeof(size32_t))));
  475. buffer.rewrite(pos);
  476. }
  477. protected:
  478. MemoryBuffer & buffer;
  479. };
  480. inline void doDataset2RowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, size32_t lenSrc, const void * src, bool isGrouped)
  481. {
  482. RtlLinkedDatasetBuilder builder(rowAllocator);
  483. Owned<ISerialStream> stream = createMemorySerialStream(src, lenSrc);
  484. CThorStreamDeserializerSource source(stream);
  485. doDeserializeRowset(builder, *deserializer, source, lenSrc, isGrouped);
  486. count = builder.getcount();
  487. rowset = builder.linkrows();
  488. }
  489. inline void doRowset2DatasetX(unsigned & tlen, void * & tgt, IOutputRowSerializer * serializer, size32_t count, byte * * rows, bool isGrouped)
  490. {
  491. MemoryBuffer buffer;
  492. CThorRtlRowSerializer out(buffer);
  493. doSerializeRowset(out, serializer, count, rows, isGrouped);
  494. rtlFree(tgt);
  495. tlen = buffer.length();
  496. tgt = buffer.detach(); // not strictly speaking correct - it should have been allocated with rtlMalloc();
  497. }
  498. extern ECLRTL_API void rtlDataset2RowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, size32_t lenSrc, const void * src, bool isGrouped)
  499. {
  500. doDataset2RowsetX(count, rowset, rowAllocator, deserializer, lenSrc, src, isGrouped);
  501. }
  502. extern ECLRTL_API void rtlDataset2RowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, size32_t lenSrc, const void * src)
  503. {
  504. doDataset2RowsetX(count, rowset, rowAllocator, deserializer, lenSrc, src, false);
  505. }
  506. extern ECLRTL_API void rtlGroupedDataset2RowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, size32_t lenSrc, const void * src)
  507. {
  508. doDataset2RowsetX(count, rowset, rowAllocator, deserializer, lenSrc, src, true);
  509. }
  510. extern ECLRTL_API void rtlRowset2DatasetX(unsigned & tlen, void * & tgt, IOutputRowSerializer * serializer, size32_t count, byte * * rows, bool isGrouped)
  511. {
  512. doRowset2DatasetX(tlen, tgt, serializer, count, rows, isGrouped);
  513. }
  514. extern ECLRTL_API void rtlRowset2DatasetX(unsigned & tlen, void * & tgt, IOutputRowSerializer * serializer, size32_t count, byte * * rows)
  515. {
  516. doRowset2DatasetX(tlen, tgt, serializer, count, rows, false);
  517. }
  518. extern ECLRTL_API void rtlGroupedRowset2DatasetX(unsigned & tlen, void * & tgt, IOutputRowSerializer * serializer, size32_t count, byte * * rows)
  519. {
  520. doRowset2DatasetX(tlen, tgt, serializer, count, rows, true);
  521. }
  522. void deserializeRowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, MemoryBuffer &in)
  523. {
  524. Owned<ISerialStream> stream = createMemoryBufferSerialStream(in);
  525. CThorStreamDeserializerSource rowSource(stream);
  526. doDeserializeRowset(count, rowset, _rowAllocator, deserializer, rowSource, false);
  527. }
  528. void deserializeGroupedRowsetX(size32_t & count, byte * * & rowset, IEngineRowAllocator * _rowAllocator, IOutputRowDeserializer * deserializer, MemoryBuffer &in)
  529. {
  530. Owned<ISerialStream> stream = createMemoryBufferSerialStream(in);
  531. CThorStreamDeserializerSource rowSource(stream);
  532. doDeserializeRowset(count, rowset, _rowAllocator, deserializer, rowSource, true);
  533. }
  534. void serializeRowsetX(size32_t count, byte * * rows, IOutputRowSerializer * serializer, MemoryBuffer & buffer)
  535. {
  536. CThorRtlRowSerializer out(buffer);
  537. rtlSerializeRowset(out, serializer, count, rows);
  538. }
  539. void serializeGroupedRowsetX(size32_t count, byte * * rows, IOutputRowSerializer * serializer, MemoryBuffer & buffer)
  540. {
  541. CThorRtlRowSerializer out(buffer);
  542. rtlSerializeGroupedRowset(out, serializer, count, rows);
  543. }
  544. void serializeRow(const void * row, IOutputRowSerializer * serializer, MemoryBuffer & buffer)
  545. {
  546. CThorRtlRowSerializer out(buffer);
  547. serializer->serialize(out, static_cast<const byte *>(row));
  548. }
  549. extern ECLRTL_API byte * rtlDeserializeBufferRow(IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, MemoryBuffer & buffer)
  550. {
  551. Owned<ISerialStream> stream = createMemoryBufferSerialStream(buffer);
  552. CThorStreamDeserializerSource source(stream);
  553. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  554. size32_t rowSize = deserializer->deserialize(rowBuilder, source);
  555. return static_cast<byte *>(const_cast<void *>(rowBuilder.finalizeRowClear(rowSize)));
  556. }
  557. extern ECLRTL_API byte * rtlDeserializeRow(IEngineRowAllocator * rowAllocator, IOutputRowDeserializer * deserializer, const void * src)
  558. {
  559. Owned<ISerialStream> stream = createMemorySerialStream(src, 0x7fffffff);
  560. CThorStreamDeserializerSource source(stream);
  561. RtlDynamicRowBuilder rowBuilder(rowAllocator);
  562. size32_t rowSize = deserializer->deserialize(rowBuilder, source);
  563. return static_cast<byte *>(const_cast<void *>(rowBuilder.finalizeRowClear(rowSize)));
  564. }
  565. extern ECLRTL_API size32_t rtlSerializeRow(size32_t lenOut, void * out, IOutputRowSerializer * serializer, const void * src)
  566. {
  567. MemoryBuffer buffer;
  568. CThorRtlRowSerializer result(buffer);
  569. buffer.setBuffer(lenOut, out, false);
  570. buffer.setWritePos(0);
  571. serializer->serialize(result, (const byte *)src);
  572. return buffer.length();
  573. }
  574. class ECLRTL_API CThorRtlBuilderSerializer : implements IRowSerializerTarget
  575. {
  576. public:
  577. CThorRtlBuilderSerializer(ARowBuilder & _builder) : builder(_builder)
  578. {
  579. offset = 0;
  580. }
  581. virtual void put(size32_t len, const void * ptr)
  582. {
  583. byte * data = builder.ensureCapacity(offset + len, "");
  584. memcpy(data+offset, ptr, len);
  585. offset += len;
  586. }
  587. virtual size32_t beginNested()
  588. {
  589. unsigned pos = offset;
  590. offset += sizeof(size32_t);
  591. return pos;
  592. }
  593. virtual void endNested(size32_t sizePos)
  594. {
  595. byte * self = builder.getSelf();
  596. *(size32_t *)(self + sizePos) = offset - (sizePos + sizeof(size32_t));
  597. }
  598. inline size32_t length() const { return offset; }
  599. protected:
  600. ARowBuilder & builder;
  601. size32_t offset;
  602. };
  603. extern ECLRTL_API size32_t rtlDeserializeToBuilder(ARowBuilder & builder, IOutputRowDeserializer * deserializer, const void * src)
  604. {
  605. Owned<ISerialStream> stream = createMemorySerialStream(src, 0x7fffffff);
  606. CThorStreamDeserializerSource source(stream);
  607. return deserializer->deserialize(builder, source);
  608. }
  609. extern ECLRTL_API size32_t rtlSerializeToBuilder(ARowBuilder & builder, IOutputRowSerializer * serializer, const void * src)
  610. {
  611. CThorRtlBuilderSerializer target(builder);
  612. serializer->serialize(target, (const byte *)src);
  613. return target.length();
  614. }
  615. //---------------------------------------------------------------------------
  616. RtlDatasetCursor::RtlDatasetCursor(size32_t _len, const void * _data)
  617. {
  618. setDataset(_len, _data);
  619. }
  620. bool RtlDatasetCursor::exists()
  621. {
  622. return (end != buffer);
  623. }
  624. const byte * RtlDatasetCursor::first()
  625. {
  626. if (buffer != end)
  627. cur = buffer;
  628. return cur;
  629. }
  630. const byte * RtlDatasetCursor::get()
  631. {
  632. return cur;
  633. }
  634. void RtlDatasetCursor::setDataset(size32_t _len, const void * _data)
  635. {
  636. buffer = (const byte *)_data;
  637. end = buffer + _len;
  638. cur = NULL;
  639. }
  640. bool RtlDatasetCursor::isValid()
  641. {
  642. return (cur != NULL);
  643. }
  644. /*
  645. const byte * RtlDatasetCursor::next()
  646. {
  647. if (cur)
  648. {
  649. cur += getRowSize();
  650. if (cur >= end)
  651. cur = NULL;
  652. }
  653. return cur;
  654. }
  655. */
  656. //---------------------------------------------------------------------------
  657. RtlFixedDatasetCursor::RtlFixedDatasetCursor(size32_t _len, const void * _data, unsigned _recordSize) : RtlDatasetCursor(_len, _data)
  658. {
  659. recordSize = _recordSize;
  660. }
  661. RtlFixedDatasetCursor::RtlFixedDatasetCursor() : RtlDatasetCursor(0, NULL)
  662. {
  663. recordSize = 1;
  664. }
  665. size32_t RtlFixedDatasetCursor::count()
  666. {
  667. return (size32_t)((end - buffer) / recordSize);
  668. }
  669. size32_t RtlFixedDatasetCursor::getSize()
  670. {
  671. return recordSize;
  672. }
  673. void RtlFixedDatasetCursor::init(size32_t _len, const void * _data, unsigned _recordSize)
  674. {
  675. recordSize = _recordSize;
  676. setDataset(_len, _data);
  677. }
  678. const byte * RtlFixedDatasetCursor::next()
  679. {
  680. if (cur)
  681. {
  682. cur += recordSize;
  683. if (cur >= end)
  684. cur = NULL;
  685. }
  686. return cur;
  687. }
  688. const byte * RtlFixedDatasetCursor::select(unsigned idx)
  689. {
  690. cur = buffer + idx * recordSize;
  691. if (cur >= end)
  692. cur = NULL;
  693. return cur;
  694. }
  695. //---------------------------------------------------------------------------
  696. RtlVariableDatasetCursor::RtlVariableDatasetCursor(size32_t _len, const void * _data, IRecordSize & _recordSize) : RtlDatasetCursor(_len, _data)
  697. {
  698. recordSize = &_recordSize;
  699. }
  700. RtlVariableDatasetCursor::RtlVariableDatasetCursor() : RtlDatasetCursor(0, NULL)
  701. {
  702. recordSize = NULL;
  703. }
  704. void RtlVariableDatasetCursor::init(size32_t _len, const void * _data, IRecordSize & _recordSize)
  705. {
  706. recordSize = &_recordSize;
  707. setDataset(_len, _data);
  708. }
  709. size32_t RtlVariableDatasetCursor::count()
  710. {
  711. const byte * finger = buffer;
  712. unsigned c = 0;
  713. while (finger < end)
  714. {
  715. finger += recordSize->getRecordSize(finger);
  716. c++;
  717. }
  718. assertex(finger == end);
  719. return c;
  720. }
  721. size32_t RtlVariableDatasetCursor::getSize()
  722. {
  723. return recordSize->getRecordSize(cur);
  724. }
  725. const byte * RtlVariableDatasetCursor::next()
  726. {
  727. if (cur)
  728. {
  729. cur += recordSize->getRecordSize(cur);
  730. if (cur >= end)
  731. cur = NULL;
  732. }
  733. return cur;
  734. }
  735. const byte * RtlVariableDatasetCursor::select(unsigned idx)
  736. {
  737. const byte * finger = buffer;
  738. unsigned c = 0;
  739. while (finger < end)
  740. {
  741. if (c == idx)
  742. {
  743. cur = finger;
  744. return cur;
  745. }
  746. finger += recordSize->getRecordSize(finger);
  747. c++;
  748. }
  749. assertex(finger == end);
  750. cur = NULL;
  751. return NULL;
  752. }
  753. //---------------------------------------------------------------------------
  754. RtlLinkedDatasetCursor::RtlLinkedDatasetCursor(unsigned _numRows, byte * * _rows) : numRows(_numRows), rows(_rows)
  755. {
  756. cur = (unsigned)-1;
  757. }
  758. RtlLinkedDatasetCursor::RtlLinkedDatasetCursor()
  759. {
  760. numRows = 0;
  761. rows = NULL;
  762. cur = (unsigned)-1;
  763. }
  764. void RtlLinkedDatasetCursor::init(unsigned _numRows, byte * * _rows)
  765. {
  766. numRows = _numRows;
  767. rows = _rows;
  768. cur = (unsigned)-1;
  769. }
  770. const byte * RtlLinkedDatasetCursor::first()
  771. {
  772. cur = 0;
  773. return cur < numRows ? rows[cur] : NULL;
  774. }
  775. const byte * RtlLinkedDatasetCursor::get()
  776. {
  777. return cur < numRows ? rows[cur] : NULL;
  778. }
  779. bool RtlLinkedDatasetCursor::isValid()
  780. {
  781. return (cur < numRows);
  782. }
  783. const byte * RtlLinkedDatasetCursor::next()
  784. {
  785. if (cur < numRows)
  786. cur++;
  787. return cur < numRows ? rows[cur] : NULL;
  788. }
  789. const byte * RtlLinkedDatasetCursor::select(unsigned idx)
  790. {
  791. cur = idx;
  792. return cur < numRows ? rows[cur] : NULL;
  793. }
  794. //---------------------------------------------------------------------------
  795. bool rtlCheckInList(const void * lhs, IRtlDatasetCursor * cursor, ICompare * compare)
  796. {
  797. const byte * cur;
  798. for (cur = cursor->first(); cur; cur = cursor->next())
  799. {
  800. if (compare->docompare(lhs, cur) == 0)
  801. return true;
  802. }
  803. return false;
  804. }
  805. void rtlSetToSetX(bool & outIsAll, size32_t & outLen, void * & outData, bool inIsAll, size32_t inLen, void * inData)
  806. {
  807. outIsAll = inIsAll;
  808. outLen = inLen;
  809. outData = malloc(inLen);
  810. memcpy(outData, inData, inLen);
  811. }
  812. void rtlAppendSetX(bool & outIsAll, size32_t & outLen, void * & outData, bool leftIsAll, size32_t leftLen, void * leftData, bool rightIsAll, size32_t rightLen, void * rightData)
  813. {
  814. outIsAll = leftIsAll | rightIsAll;
  815. if (outIsAll)
  816. {
  817. outLen = 0;
  818. outData = NULL;
  819. }
  820. else
  821. {
  822. outLen = leftLen+rightLen;
  823. outData = malloc(outLen);
  824. memcpy(outData, leftData, leftLen);
  825. memcpy((byte*)outData+leftLen, rightData, rightLen);
  826. }
  827. }
  828. //------------------------------------------------------------------------------
  829. RtlCompoundIterator::RtlCompoundIterator()
  830. {
  831. ok = false;
  832. numLevels = 0;
  833. iters = NULL;
  834. cursors = NULL;
  835. }
  836. RtlCompoundIterator::~RtlCompoundIterator()
  837. {
  838. delete [] iters;
  839. delete [] cursors;
  840. }
  841. void RtlCompoundIterator::addIter(unsigned idx, IRtlDatasetSimpleCursor * iter, byte * * cursor)
  842. {
  843. assertex(idx < numLevels);
  844. iters[idx] = iter;
  845. cursors[idx] = cursor;
  846. }
  847. void RtlCompoundIterator::init(unsigned _numLevels)
  848. {
  849. numLevels = _numLevels;
  850. iters = new IRtlDatasetSimpleCursor * [numLevels];
  851. cursors = new byte * * [numLevels];
  852. }
  853. //Could either duplicate this function, N times, or have it as a helper function that accesses pre-defined virtuals.
  854. bool RtlCompoundIterator::first(unsigned level)
  855. {
  856. IRtlDatasetSimpleCursor * curIter = iters[level];
  857. if (level == 0)
  858. {
  859. const byte * cur = curIter->first();
  860. setCursor(level, cur);
  861. return (cur != NULL);
  862. }
  863. if (!first(level-1))
  864. return false;
  865. loop
  866. {
  867. const byte * cur = curIter->first();
  868. if (cur)
  869. {
  870. setCursor(level, cur);
  871. return true;
  872. }
  873. if (!next(level-1))
  874. return false;
  875. }
  876. }
  877. bool RtlCompoundIterator::next(unsigned level)
  878. {
  879. IRtlDatasetSimpleCursor * curIter = iters[level];
  880. const byte * cur = curIter->next();
  881. if (cur)
  882. {
  883. setCursor(level, cur);
  884. return true;
  885. }
  886. if (level == 0)
  887. return false;
  888. loop
  889. {
  890. if (!next(level-1))
  891. return false;
  892. const byte * cur = curIter->first();
  893. if (cur)
  894. {
  895. setCursor(level, cur);
  896. return true;
  897. }
  898. }
  899. }
  900. //------------------------------------------------------------------------------
  901. void RtlSimpleIterator::addIter(unsigned idx, IRtlDatasetSimpleCursor * _iter, byte * * _cursor)
  902. {
  903. assertex(idx == 0);
  904. iter = _iter;
  905. cursor = _cursor;
  906. *cursor = NULL;
  907. }
  908. bool RtlSimpleIterator::first()
  909. {
  910. const byte * cur = iter->first();
  911. *cursor = (byte *)cur;
  912. return (cur != NULL);
  913. }
  914. bool RtlSimpleIterator::next()
  915. {
  916. const byte * cur = iter->next();
  917. *cursor = (byte *)cur;
  918. return (cur != NULL);
  919. }