thorcommon.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  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 "jexcept.hpp"
  15. #include "jmisc.hpp"
  16. #include "jthread.hpp"
  17. #include "jsocket.hpp"
  18. #include "jprop.hpp"
  19. #include "jdebug.hpp"
  20. #include "jlzw.hpp"
  21. #include "junicode.hpp"
  22. #include "eclhelper.hpp"
  23. #include "thorcommon.ipp"
  24. #include "eclrtl.hpp"
  25. #include "rtlread_imp.hpp"
  26. #include "thorstep.hpp"
  27. #define ROWAGG_PERROWOVERHEAD (sizeof(AggregateRowBuilder))
  28. RowAggregator::RowAggregator(IHThorHashAggregateExtra &_extra, IHThorRowAggregator & _helper) : helper(_helper)
  29. {
  30. comparer = _extra.queryCompareRowElement();
  31. hasher = _extra.queryHash();
  32. elementHasher = _extra.queryHashElement();
  33. elementComparer = _extra.queryCompareElements();
  34. cursor = NULL;
  35. eof = false;
  36. totalSize = overhead = 0;
  37. }
  38. RowAggregator::~RowAggregator()
  39. {
  40. reset();
  41. }
  42. void RowAggregator::start(IEngineRowAllocator *_rowAllocator)
  43. {
  44. rowAllocator.set(_rowAllocator);
  45. }
  46. void RowAggregator::reset()
  47. {
  48. while (!eof)
  49. {
  50. AggregateRowBuilder *n = nextResult();
  51. if (n)
  52. n->Release();
  53. }
  54. SuperHashTable::releaseAll();
  55. eof = false;
  56. cursor = NULL;
  57. rowAllocator.clear();
  58. totalSize = overhead = 0;
  59. }
  60. AggregateRowBuilder &RowAggregator::addRow(const void * row)
  61. {
  62. AggregateRowBuilder *result;
  63. unsigned hash = hasher->hash(row);
  64. void * match = find(hash, row);
  65. if (match)
  66. {
  67. result = static_cast<AggregateRowBuilder *>(match);
  68. totalSize -= result->querySize();
  69. size32_t sz = helper.processNext(*result, row);
  70. result->setSize(sz);
  71. totalSize += sz;
  72. }
  73. else
  74. {
  75. Owned<AggregateRowBuilder> rowBuilder = new AggregateRowBuilder(rowAllocator, hash);
  76. helper.clearAggregate(*rowBuilder);
  77. size32_t sz = helper.processFirst(*rowBuilder, row);
  78. rowBuilder->setSize(sz);
  79. result = rowBuilder.getClear();
  80. addNew(result, hash);
  81. totalSize += sz;
  82. overhead += ROWAGG_PERROWOVERHEAD;
  83. }
  84. return *result;
  85. }
  86. void RowAggregator::mergeElement(const void * otherElement)
  87. {
  88. unsigned hash = elementHasher->hash(otherElement);
  89. void * match = findElement(hash, otherElement);
  90. if (match)
  91. {
  92. AggregateRowBuilder *rowBuilder = static_cast<AggregateRowBuilder *>(match);
  93. totalSize -= rowBuilder->querySize();
  94. size32_t sz = helper.mergeAggregate(*rowBuilder, otherElement);
  95. rowBuilder->setSize(sz);
  96. totalSize += sz;
  97. }
  98. else
  99. {
  100. Owned<AggregateRowBuilder> rowBuilder = new AggregateRowBuilder(rowAllocator, hash);
  101. rowBuilder->setSize(cloneRow(*rowBuilder, otherElement, rowAllocator->queryOutputMeta()));
  102. addNew(rowBuilder.getClear(), hash);
  103. }
  104. }
  105. const void * RowAggregator::getFindParam(const void *et) const
  106. {
  107. // Slightly odd name for this function... it actually gets the comparable element
  108. const AggregateRowBuilder *rb = static_cast<const AggregateRowBuilder*>(et);
  109. return rb->row();
  110. }
  111. bool RowAggregator::matchesFindParam(const void *et, const void *key, unsigned fphash) const
  112. {
  113. if (fphash != hashFromElement(et))
  114. return false;
  115. // et = element in the table (an AggregateRowBuilder) key = new row (in input row layout).
  116. return comparer->docompare(key, getFindParam(et)) == 0;
  117. }
  118. bool RowAggregator::matchesElement(const void *et, const void * searchET) const
  119. {
  120. return elementComparer->docompare(getFindParam(et), searchET) == 0;
  121. }
  122. AggregateRowBuilder *RowAggregator::nextResult()
  123. {
  124. void *ret = next(cursor);
  125. if (!ret)
  126. {
  127. eof = true;
  128. return NULL;
  129. }
  130. cursor = ret;
  131. return static_cast<AggregateRowBuilder *>(ret);
  132. }
  133. //=====================================================================================================
  134. void CStreamMerger::fillheap(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
  135. {
  136. assertex(activeInputs == 0);
  137. for(unsigned i = 0; i < numInputs; i++)
  138. if(pullInput(i, seek, numFields, stepExtra))
  139. mergeheap[activeInputs++] = i;
  140. }
  141. void CStreamMerger::permute(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
  142. {
  143. // the tree structure: element p has children p*2+1 and p*2+2, or element c has parent (unsigned)(c-1)/2
  144. // the heap property: no element should be smaller than its parent
  145. // the dedup variant: if(dedup), the top of the heap should also not be equal to either child
  146. // the method: establish this by starting with the parent of the bottom element and working up to the top element, sifting each down to its correct place
  147. if (activeInputs >= 2)
  148. for(unsigned p = (activeInputs-2)/2; p > 0; --p)
  149. siftDown(p);
  150. if(dedup)
  151. siftDownDedupTop(seek, numFields, stepExtra);
  152. else
  153. siftDown(0);
  154. }
  155. const void * CStreamMerger::consumeTop()
  156. {
  157. unsigned top = mergeheap[0];
  158. if (!pullConsumes)
  159. consumeInput(top);
  160. const void *next = pending[top];
  161. pending[top] = NULL;
  162. return next;
  163. }
  164. bool CStreamMerger::ensureNext(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra * stepExtra)
  165. {
  166. //wasCompleteMatch must be initialised from the actual row returned. (See bug #30388)
  167. if (first)
  168. {
  169. fillheap(seek, numFields, stepExtra);
  170. permute(seek, numFields, stepExtra);
  171. first = false;
  172. if (activeInputs == 0)
  173. return false;
  174. unsigned top = mergeheap[0];
  175. wasCompleteMatch = pendingMatches[top];
  176. return true;
  177. }
  178. while (activeInputs)
  179. {
  180. unsigned top = mergeheap[0];
  181. const void *next = pending[top];
  182. if (next)
  183. {
  184. if (seek)
  185. {
  186. int c = rangeCompare->docompare(next, seek, numFields);
  187. if (c >= 0)
  188. {
  189. if (stepExtra->returnMismatches() && (c > 0))
  190. {
  191. wasCompleteMatch = pendingMatches[top];
  192. return true;
  193. }
  194. else
  195. {
  196. if (pendingMatches[top])
  197. return true;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. if (pendingMatches[top])
  204. return true;
  205. }
  206. skipInput(top);
  207. }
  208. if(!pullInput(top, seek, numFields, stepExtra))
  209. if(!promote(0))
  210. return false;
  211. // we have changed the element at the top of the heap, so need to sift it down to maintain the heap property
  212. if(dedup)
  213. siftDownDedupTop(seek, numFields, stepExtra);
  214. else
  215. siftDown(0);
  216. }
  217. return false;
  218. }
  219. bool CStreamMerger::ensureNext()
  220. {
  221. bool isCompleteMatch = true;
  222. return ensureNext(NULL, 0, isCompleteMatch, NULL);
  223. }
  224. void CStreamMerger::permute()
  225. {
  226. permute(NULL, 0, NULL);
  227. }
  228. bool CStreamMerger::promote(unsigned p)
  229. {
  230. activeInputs--;
  231. if(activeInputs == p)
  232. return false;
  233. mergeheap[p] = mergeheap[activeInputs];
  234. return true;
  235. }
  236. void CStreamMerger::siftDownDedupTop(const void * seek, unsigned numFields, const SmartStepExtra * stepExtra)
  237. {
  238. // same as siftDown(0), except that it also ensures that the top of the heap is not equal to either of its children
  239. if(activeInputs < 2)
  240. return;
  241. unsigned c = 1;
  242. int childcmp = 1;
  243. if(activeInputs >= 3)
  244. {
  245. childcmp = compare->docompare(pending[mergeheap[2]], pending[mergeheap[1]]);
  246. if(childcmp < 0)
  247. c = 2;
  248. }
  249. int cmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
  250. if(cmp > 0)
  251. return;
  252. // the following loop ensures the correct property holds on the smaller branch, and that childcmp==0 iff the top matches the other branch
  253. while(cmp <= 0)
  254. {
  255. if(cmp == 0)
  256. {
  257. if(mergeheap[c] < mergeheap[0])
  258. {
  259. unsigned r = mergeheap[c];
  260. mergeheap[c] = mergeheap[0];
  261. mergeheap[0] = r;
  262. }
  263. unsigned top = mergeheap[c];
  264. skipInput(top);
  265. if(!pullInput(top, seek, numFields, stepExtra))
  266. if(!promote(c))
  267. break;
  268. siftDown(c);
  269. }
  270. else
  271. {
  272. unsigned r = mergeheap[c];
  273. mergeheap[c] = mergeheap[0];
  274. mergeheap[0] = r;
  275. if(siftDown(c))
  276. break;
  277. }
  278. cmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
  279. }
  280. // the following loop ensures the uniqueness property holds on the other branch too
  281. c = 3-c;
  282. if(activeInputs <= c)
  283. return;
  284. while(childcmp == 0)
  285. {
  286. if(mergeheap[c] < mergeheap[0])
  287. {
  288. unsigned r = mergeheap[c];
  289. mergeheap[c] = mergeheap[0];
  290. mergeheap[0] = r;
  291. }
  292. unsigned top = mergeheap[c];
  293. skipInput(top);
  294. if(!pullInput(top, seek, numFields, stepExtra))
  295. if(!promote(c))
  296. break;
  297. siftDown(c);
  298. childcmp = compare->docompare(pending[mergeheap[c]], pending[mergeheap[0]]);
  299. }
  300. }
  301. void CStreamMerger::cleanup()
  302. {
  303. clearPending();
  304. delete [] pending;
  305. pending = NULL;
  306. delete [] pendingMatches;
  307. pendingMatches = NULL;
  308. delete [] mergeheap;
  309. mergeheap = NULL;
  310. }
  311. void CStreamMerger::clearPending()
  312. {
  313. if (pending && activeInputs)
  314. {
  315. for(unsigned i = 0; i < numInputs; i++)
  316. {
  317. if (pullConsumes)
  318. releaseRow(pending[i]);
  319. pending[i] = NULL;
  320. }
  321. activeInputs = 0;
  322. }
  323. first = true;
  324. }
  325. CStreamMerger::CStreamMerger(bool _pullConsumes)
  326. {
  327. pending = NULL;
  328. pendingMatches = NULL;
  329. mergeheap = NULL;
  330. compare = NULL;
  331. rangeCompare = NULL;
  332. dedup = false;
  333. activeInputs = 0;
  334. pullConsumes = _pullConsumes;
  335. numInputs = 0;
  336. first = true;
  337. }
  338. CStreamMerger::~CStreamMerger()
  339. {
  340. //can't call cleanup() because virtual releaseRow() won't be defined.
  341. // NOTE: use assert rather than assertex as exceptions from within destructors are not handled well.
  342. assert(!pending && !mergeheap);
  343. }
  344. void CStreamMerger::init(ICompare * _compare, bool _dedup, IRangeCompare * _rangeCompare)
  345. {
  346. compare = _compare;
  347. dedup = _dedup;
  348. rangeCompare = _rangeCompare;
  349. }
  350. void CStreamMerger::initInputs(unsigned _numInputs)
  351. {
  352. assertex(!pending); // cleanup should have been called before reinitializing
  353. numInputs = _numInputs;
  354. mergeheap = new unsigned[numInputs];
  355. pending = new const void *[numInputs];
  356. pendingMatches = new bool [numInputs];
  357. for (unsigned i = 0; i < numInputs; i++)
  358. pending[i] = NULL;
  359. activeInputs = 0;
  360. first = true;
  361. }
  362. void CStreamMerger::consumeInput(unsigned i)
  363. {
  364. //should be over-ridden if pullConsumes is false;
  365. throwUnexpected();
  366. }
  367. void CStreamMerger::skipInput(unsigned i)
  368. {
  369. if (!pullConsumes)
  370. consumeInput(i);
  371. releaseRow(pending[i]);
  372. pending[i] = NULL;
  373. }
  374. void CStreamMerger::primeRows(const void * * rows)
  375. {
  376. assertex(first && (activeInputs == 0));
  377. first = false;
  378. for(unsigned i = 0; i < numInputs; i++)
  379. {
  380. if ((pending[i] = rows[i]) != NULL)
  381. {
  382. mergeheap[activeInputs++] = i;
  383. pendingMatches[i] = true;
  384. }
  385. }
  386. permute();
  387. }
  388. const void * CStreamMerger::nextRow()
  389. {
  390. if (ensureNext())
  391. return consumeTop();
  392. return NULL;
  393. }
  394. const void * CStreamMerger::queryNextRow()
  395. {
  396. if (ensureNext())
  397. return pending[mergeheap[0]];
  398. return NULL;
  399. }
  400. unsigned CStreamMerger::queryNextInput()
  401. {
  402. if (ensureNext())
  403. return mergeheap[0];
  404. return NotFound;
  405. }
  406. const void * CStreamMerger::nextRowGE(const void * seek, unsigned numFields, bool & wasCompleteMatch, const SmartStepExtra & stepExtra)
  407. {
  408. if (ensureNext(seek, numFields, wasCompleteMatch, &stepExtra))
  409. return consumeTop();
  410. return NULL;
  411. }
  412. void CStreamMerger::skipRow()
  413. {
  414. assertex(!first);
  415. skipInput(mergeheap[0]);
  416. }
  417. //=====================================================================================================
  418. CThorDemoRowSerializer::CThorDemoRowSerializer(MemoryBuffer & _buffer) : buffer(_buffer)
  419. {
  420. nesting = 0;
  421. }
  422. void CThorDemoRowSerializer::put(size32_t len, const void * ptr)
  423. {
  424. buffer.append(len, ptr);
  425. //ok to flush if nesting == 0;
  426. }
  427. size32_t CThorDemoRowSerializer::beginNested()
  428. {
  429. nesting++;
  430. unsigned pos = buffer.length();
  431. buffer.append((size32_t)0);
  432. return pos;
  433. }
  434. void CThorDemoRowSerializer::endNested(size32_t sizePos)
  435. {
  436. unsigned pos = buffer.length();
  437. buffer.rewrite(sizePos);
  438. buffer.append((size32_t)(pos - (sizePos + sizeof(size32_t))));
  439. buffer.rewrite(pos);
  440. nesting--;
  441. }
  442. IOutputRowSerializer * CachedOutputMetaData::createRowSerializer(ICodeContext * ctx, unsigned activityId) const
  443. {
  444. if (metaFlags & (MDFhasserialize|MDFneedserialize))
  445. return meta->createRowSerializer(ctx, activityId);
  446. if (isFixedSize())
  447. return new CSimpleFixedRowSerializer(getFixedSize());
  448. return new CSimpleVariableRowSerializer(this);
  449. }
  450. IOutputRowDeserializer * CachedOutputMetaData::createRowDeserializer(ICodeContext * ctx, unsigned activityId) const
  451. {
  452. if (metaFlags & (MDFhasserialize|MDFneedserialize))
  453. return meta->createRowDeserializer(ctx, activityId);
  454. if (isFixedSize())
  455. return new CSimpleFixedRowDeserializer(getFixedSize());
  456. assertex(!"createRowDeserializer variable meta has no serializer");
  457. //return new CSimpleVariableRowDeserializer(this);
  458. return NULL;
  459. }
  460. void CSizingSerializer::put(size32_t len, const void * ptr)
  461. {
  462. totalsize += len;
  463. }
  464. size32_t CSizingSerializer::beginNested()
  465. {
  466. totalsize += sizeof(size32_t);
  467. return totalsize;
  468. }
  469. void CSizingSerializer::endNested(size32_t position)
  470. {
  471. }
  472. void CMemoryRowSerializer::put(size32_t len, const void * ptr)
  473. {
  474. buffer.append(len, ptr);
  475. }
  476. size32_t CMemoryRowSerializer::beginNested()
  477. {
  478. nesting++;
  479. unsigned pos = buffer.length();
  480. buffer.append((size32_t)0);
  481. return pos;
  482. }
  483. void CMemoryRowSerializer::endNested(size32_t sizePos)
  484. {
  485. size32_t sz = buffer.length()-(sizePos + sizeof(size32_t));
  486. buffer.writeDirect(sizePos,sizeof(sz),&sz);
  487. nesting--;
  488. }
  489. static void ensureClassesAreNotAbstract()
  490. {
  491. MemoryBuffer temp;
  492. CThorStreamDeserializerSource x1(NULL);
  493. CThorContiguousRowBuffer x2(NULL);
  494. CSizingSerializer x3;
  495. CMemoryRowSerializer x4(temp);
  496. }
  497. //=====================================================================================================
  498. class ChildRowLinkerWalker : implements IIndirectMemberVisitor
  499. {
  500. public:
  501. virtual void visitRowset(size32_t count, byte * * rows)
  502. {
  503. rtlLinkRowset(rows);
  504. }
  505. virtual void visitRow(const byte * row)
  506. {
  507. rtlLinkRow(row);
  508. }
  509. };
  510. //Deprecated - should use the second definition below
  511. void * cloneRow(IEngineRowAllocator * allocator, const void * row, size32_t &sizeout)
  512. {
  513. IOutputMetaData * meta = allocator->queryOutputMeta();
  514. void * ret = allocator->createRow();
  515. sizeout = meta->getRecordSize(row); // TBD could be better?
  516. //GH this may no longer be big enough
  517. memcpy(ret, row, sizeout);
  518. if (meta->getMetaFlags() & MDFneedserialize)
  519. {
  520. ChildRowLinkerWalker walker;
  521. meta->walkIndirectMembers(static_cast<const byte *>(ret), walker);
  522. }
  523. //NB: Does not call finalizeRow()...
  524. return ret;
  525. }
  526. //the visitor callback is used to ensure link counts for children are updated.
  527. size32_t cloneRow(ARowBuilder & rowBuilder, const void * row, IOutputMetaData * meta)
  528. {
  529. size32_t rowSize = meta->getRecordSize(row); // TBD could be better?
  530. byte * self = rowBuilder.ensureCapacity(rowSize, NULL);
  531. memcpy(self, row, rowSize);
  532. if (meta->getMetaFlags() & MDFneedserialize)
  533. {
  534. ChildRowLinkerWalker walker;
  535. meta->walkIndirectMembers(self, walker);
  536. }
  537. return rowSize;
  538. }
  539. //---------------------------------------------------------------------------------------------------
  540. extern const char * getActivityText(ThorActivityKind kind)
  541. {
  542. switch (kind)
  543. {
  544. case TAKnone: return "None";
  545. case TAKdiskwrite: return "Disk Write";
  546. case TAKsort: return "Sort";
  547. case TAKdedup: return "Dedup";
  548. case TAKfilter: return "Filter";
  549. case TAKsplit: return "Split";
  550. case TAKproject: return "Project";
  551. case TAKrollup: return "Rollup";
  552. case TAKiterate: return "Iterate";
  553. case TAKaggregate: return "Aggregate";
  554. case TAKhashaggregate: return "Hash Aggregate";
  555. case TAKfirstn: return "Firstn";
  556. case TAKsample: return "Sample";
  557. case TAKdegroup: return "Degroup";
  558. case TAKjoin: return "Join";
  559. case TAKhashjoin: return "Hash Join";
  560. case TAKlookupjoin: return "Lookup Join";
  561. case TAKselfjoin: return "Self Join";
  562. case TAKkeyedjoin: return "Keyed Join";
  563. case TAKgroup: return "Group";
  564. case TAKworkunitwrite: return "Output";
  565. case TAKfunnel: return "Funnel";
  566. case TAKapply: return "Apply";
  567. case TAKtemptable: return "Inline Dataset";
  568. case TAKtemprow: return "Inline Row";
  569. case TAKhashdistribute: return "Hash Distribute";
  570. case TAKhashdedup: return "Hash Dedup";
  571. case TAKnormalize: return "Normalize";
  572. case TAKremoteresult: return "Remote Result";
  573. case TAKpull: return "Pull";
  574. case TAKdenormalize: return "Denormalize";
  575. case TAKnormalizechild: return "Normalize Child";
  576. case TAKchilddataset: return "Child Dataset";
  577. case TAKselectn: return "Select Nth";
  578. case TAKenth: return "Enth";
  579. case TAKif: return "If";
  580. case TAKnull: return "Null";
  581. case TAKdistribution: return "Distribution";
  582. case TAKcountproject: return "Count Project";
  583. case TAKchoosesets: return "Choose Sets";
  584. case TAKpiperead: return "Pipe Read";
  585. case TAKpipewrite: return "Pipe Write";
  586. case TAKcsvwrite: return "Csv Write";
  587. case TAKpipethrough: return "Pipe Through";
  588. case TAKindexwrite: return "Index Write";
  589. case TAKchoosesetsenth: return "Choose Sets Enth";
  590. case TAKchoosesetslast: return "Choose Sets Last";
  591. case TAKfetch: return "Fetch";
  592. case TAKhashdenormalize: return "Hash Denormalize";
  593. case TAKworkunitread: return "Read";
  594. case TAKthroughaggregate: return "Through Aggregate";
  595. case TAKspill: return "Spill";
  596. case TAKcase: return "Case";
  597. case TAKlimit: return "Limit";
  598. case TAKcsvfetch: return "Csv Fetch";
  599. case TAKxmlwrite: return "Xml Write";
  600. case TAKparse: return "Parse";
  601. case TAKcountdisk: return "Count Disk";
  602. case TAKsideeffect: return "Simple Action";
  603. case TAKtopn: return "Top N";
  604. case TAKmerge: return "Merge";
  605. case TAKxmlfetch: return "Xml Fetch";
  606. case TAKxmlparse: return "Parse Xml";
  607. case TAKkeyeddistribute: return "Keyed Distribute";
  608. case TAKjoinlight: return "Lightweight Join";
  609. case TAKalljoin: return "All Join";
  610. case TAKsoap_rowdataset: return "SOAP dataset";
  611. case TAKsoap_rowaction: return "SOAP action";
  612. case TAKsoap_datasetdataset: return "SOAP dataset";
  613. case TAKsoap_datasetaction: return "SOAP action";
  614. case TAKkeydiff: return "Key Difference";
  615. case TAKkeypatch: return "Key Patch";
  616. case TAKkeyeddenormalize: return "Keyed Denormalize";
  617. case TAKsequential: return "Sequential";
  618. case TAKparallel: return "Parallel";
  619. case TAKchilditerator: return "Child Dataset";
  620. case TAKdatasetresult: return "Dataset Result";
  621. case TAKrowresult: return "Row Result";
  622. case TAKchildif: return "If";
  623. case TAKpartition: return "Partition Distribute";
  624. case TAKsubgraph: return "Sub Graph";
  625. case TAKlocalgraph: return "Local Graph";
  626. case TAKifaction: return "If Action";
  627. case TAKemptyaction: return "Empty Action";
  628. case TAKskiplimit: return "Skip Limit";
  629. case TAKdiskread: return "Disk Read";
  630. case TAKdisknormalize: return "Disk Normalize";
  631. case TAKdiskaggregate: return "Disk Aggregate";
  632. case TAKdiskcount: return "Disk Count";
  633. case TAKdiskgroupaggregate: return "Disk Grouped Aggregate";
  634. case TAKindexread: return "Index Read";
  635. case TAKindexnormalize: return "Index Normalize";
  636. case TAKindexaggregate: return "Index Aggregate";
  637. case TAKindexcount: return "Index Count";
  638. case TAKindexgroupaggregate: return "Index Grouped Aggregate";
  639. case TAKchildnormalize: return "Child Normalize";
  640. case TAKchildaggregate: return "Child Aggregate";
  641. case TAKchildgroupaggregate: return "Child Grouped Aggregate";
  642. case TAKchildthroughnormalize: return "Normalize";
  643. case TAKcsvread: return "Csv Read";
  644. case TAKxmlread: return "Xml Read";
  645. case TAKlocalresultread: return "Read Local Result";
  646. case TAKlocalresultwrite: return "Local Result";
  647. case TAKcombine: return "Combine";
  648. case TAKregroup: return "Regroup";
  649. case TAKrollupgroup: return "Rollup Group";
  650. case TAKcombinegroup: return "Combine Group";
  651. case TAKlookupdenormalize: return "Lookup Denormalize";
  652. case TAKalldenormalize: return "All Denormalize";
  653. case TAKdenormalizegroup: return "Denormalize Group";
  654. case TAKhashdenormalizegroup: return "Hash Denormalize Group";
  655. case TAKlookupdenormalizegroup: return "Lookup Denormalize Group";
  656. case TAKkeyeddenormalizegroup: return "Keyed Denormalize Group";
  657. case TAKalldenormalizegroup: return "All Denormalize Group";
  658. case TAKlocalresultspill: return "Spill Local Result";
  659. case TAKsimpleaction: return "Action";
  660. case TAKloopcount: return "Loop";
  661. case TAKlooprow: return "Loop";
  662. case TAKloopdataset: return "Loop";
  663. case TAKchildcase: return "Case";
  664. case TAKremotegraph: return "Remote";
  665. case TAKlibrarycall: return "Library Call";
  666. case TAKrawiterator: return "Child Dataset";
  667. case TAKlocalstreamread: return "Read Input";
  668. case TAKprocess: return "Process";
  669. case TAKgraphloop: return "Graph";
  670. case TAKparallelgraphloop: return "Graph";
  671. case TAKgraphloopresultread: return "Graph Input";
  672. case TAKgraphloopresultwrite: return "Graph Result";
  673. case TAKgrouped: return "Grouped";
  674. case TAKsorted: return "Sorted";
  675. case TAKdistributed: return "Distributed";
  676. case TAKnwayjoin: return "Join";
  677. case TAKnwaymerge: return "Merge";
  678. case TAKnwaymergejoin: return "Merge Join";
  679. case TAKnwayinput: return "Nway Input";
  680. case TAKnwaygraphloopresultread: return "Nway Graph Input";
  681. case TAKnwayselect: return "Select Nway Input";
  682. case TAKnonempty: return "Non Empty";
  683. case TAKcreaterowlimit: return "OnFail Limit";
  684. case TAKexistsaggregate: return "Exists";
  685. case TAKcountaggregate: return "Count";
  686. case TAKprefetchproject: return "Prefetch Project";
  687. case TAKprefetchcountproject: return "Prefetch Count Project";
  688. case TAKfiltergroup: return "Filter Group";
  689. case TAKmemoryspillread: return "Read Spill";
  690. case TAKmemoryspillwrite: return "Write Spill";
  691. case TAKmemoryspillsplit: return "Spill";
  692. case TAKsection: return "Section";
  693. case TAKlinkedrawiterator: return "Child Dataset";
  694. case TAKnormalizelinkedchild: return "Normalize";
  695. case TAKfilterproject: return "Filtered Project";
  696. case TAKcatch: return "Catch";
  697. case TAKskipcatch: return "Skip Catch";
  698. case TAKcreaterowcatch: return "OnFail Catch";
  699. case TAKsectioninput: return "Section Input";
  700. case TAKindexgroupcount: return "Index Grouped Count";
  701. case TAKindexgroupexists: return "Index Grouped Exists";
  702. case TAKhashdistributemerge: return "Distribute Merge";
  703. case TAKselfjoinlight: return "Lightweight Self Join";
  704. case TAKwhen_dataset: return "When";
  705. case TAKhttp_rowdataset: return "HTTP dataset";
  706. case TAKstreamediterator: return "Streamed Dataset";
  707. case TAKexternalsource: return "User Source";
  708. case TAKexternalsink: return "User Output";
  709. case TAKexternalprocess: return "User Proceess";
  710. case TAKwhen_action: return "When";
  711. }
  712. throwUnexpected();
  713. }
  714. extern bool isActivitySource(ThorActivityKind kind)
  715. {
  716. switch (kind)
  717. {
  718. case TAKpiperead:
  719. case TAKtemptable:
  720. case TAKtemprow:
  721. case TAKworkunitread:
  722. case TAKnull:
  723. case TAKsideeffect:
  724. case TAKsoap_rowdataset:
  725. case TAKsoap_rowaction:
  726. case TAKkeydiff:
  727. case TAKkeypatch:
  728. case TAKchilditerator:
  729. case TAKlocalgraph:
  730. case TAKemptyaction:
  731. case TAKdiskread:
  732. case TAKdisknormalize:
  733. case TAKdiskaggregate:
  734. case TAKdiskcount:
  735. case TAKdiskgroupaggregate:
  736. case TAKindexread:
  737. case TAKindexnormalize:
  738. case TAKindexaggregate:
  739. case TAKindexcount:
  740. case TAKindexgroupaggregate:
  741. case TAKchildnormalize:
  742. case TAKchildaggregate:
  743. case TAKchildgroupaggregate:
  744. case TAKcsvread:
  745. case TAKxmlread:
  746. case TAKlocalresultread:
  747. case TAKsimpleaction:
  748. case TAKrawiterator:
  749. case TAKlocalstreamread:
  750. case TAKgraphloopresultread:
  751. case TAKnwaygraphloopresultread:
  752. case TAKlinkedrawiterator:
  753. case TAKindexgroupexists:
  754. case TAKindexgroupcount:
  755. case TAKstreamediterator:
  756. case TAKexternalsource:
  757. return true;
  758. }
  759. return false;
  760. }
  761. extern bool isActivitySink(ThorActivityKind kind)
  762. {
  763. switch (kind)
  764. {
  765. case TAKdiskwrite:
  766. case TAKworkunitwrite:
  767. case TAKapply:
  768. case TAKremoteresult:
  769. case TAKdistribution:
  770. case TAKpipewrite:
  771. case TAKcsvwrite:
  772. case TAKindexwrite:
  773. case TAKxmlwrite:
  774. case TAKsoap_rowaction:
  775. case TAKsoap_datasetaction:
  776. case TAKkeydiff:
  777. case TAKkeypatch:
  778. case TAKdatasetresult:
  779. case TAKrowresult:
  780. case TAKemptyaction:
  781. case TAKlocalresultwrite:
  782. case TAKgraphloopresultwrite:
  783. case TAKsimpleaction:
  784. case TAKexternalsink:
  785. case TAKifaction:
  786. case TAKparallel:
  787. case TAKsequential:
  788. case TAKwhen_action:
  789. return true;
  790. }
  791. return false;
  792. }
  793. //------------------------------------------------------------------------------------------------
  794. byte * CStaticRowBuilder::ensureCapacity(size32_t required, const char * fieldName)
  795. {
  796. if (required <= maxLength)
  797. return static_cast<byte *>(self);
  798. rtlReportFieldOverflow(required, maxLength, fieldName);
  799. return NULL;
  800. }
  801. //=====================================================================================================
  802. CThorContiguousRowBuffer::CThorContiguousRowBuffer(ISerialStream * _in) : in(_in)
  803. {
  804. buffer = NULL;
  805. maxOffset = 0;
  806. readOffset = 0;
  807. }
  808. void CThorContiguousRowBuffer::doRead(size32_t len, void * ptr)
  809. {
  810. ensureAccessible(readOffset + len);
  811. memcpy(ptr, buffer+readOffset, len);
  812. readOffset += len;
  813. }
  814. size32_t CThorContiguousRowBuffer::read(size32_t len, void * ptr)
  815. {
  816. doRead(len, ptr);
  817. return len;
  818. }
  819. size32_t CThorContiguousRowBuffer::readSize()
  820. {
  821. size32_t value;
  822. doRead(sizeof(value), &value);
  823. return value;
  824. }
  825. size32_t CThorContiguousRowBuffer::readPackedInt(void * ptr)
  826. {
  827. size32_t size = sizePackedInt();
  828. doRead(size, ptr);
  829. return size;
  830. }
  831. size32_t CThorContiguousRowBuffer::readUtf8(ARowBuilder & target, size32_t offset, size32_t fixedSize, size32_t len)
  832. {
  833. if (len == 0)
  834. return 0;
  835. size32_t size = sizeUtf8(len);
  836. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  837. doRead(size, self+offset);
  838. return size;
  839. }
  840. size32_t CThorContiguousRowBuffer::readVStr(ARowBuilder & target, size32_t offset, size32_t fixedSize)
  841. {
  842. size32_t size = sizeVStr();
  843. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  844. doRead(size, self+offset);
  845. return size;
  846. }
  847. size32_t CThorContiguousRowBuffer::readVUni(ARowBuilder & target, size32_t offset, size32_t fixedSize)
  848. {
  849. size32_t size = sizeVUni();
  850. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  851. doRead(size, self+offset);
  852. return size;
  853. }
  854. size32_t CThorContiguousRowBuffer::sizePackedInt()
  855. {
  856. ensureAccessible(readOffset+1);
  857. return rtlGetPackedSizeFromFirst(buffer[readOffset]);
  858. }
  859. size32_t CThorContiguousRowBuffer::sizeUtf8(size32_t len)
  860. {
  861. if (len == 0)
  862. return 0;
  863. //The len is the number of utf characters, size depends on which characters are included.
  864. size32_t nextOffset = readOffset;
  865. while (len)
  866. {
  867. ensureAccessible(nextOffset+1);
  868. for (;nextOffset < maxOffset;)
  869. {
  870. nextOffset += readUtf8Size(buffer+nextOffset); // This function only accesses the first byte
  871. if (--len == 0)
  872. break;
  873. }
  874. }
  875. return nextOffset - readOffset;
  876. }
  877. size32_t CThorContiguousRowBuffer::sizeVStr()
  878. {
  879. size32_t nextOffset = readOffset;
  880. loop
  881. {
  882. ensureAccessible(nextOffset+1);
  883. for (; nextOffset < maxOffset; nextOffset++)
  884. {
  885. if (buffer[nextOffset] == 0)
  886. return (nextOffset + 1) - readOffset;
  887. }
  888. }
  889. }
  890. size32_t CThorContiguousRowBuffer::sizeVUni()
  891. {
  892. size32_t nextOffset = readOffset;
  893. const size32_t sizeOfUChar = 2;
  894. loop
  895. {
  896. ensureAccessible(nextOffset+sizeOfUChar);
  897. for (; nextOffset+1 < maxOffset; nextOffset += sizeOfUChar)
  898. {
  899. if (buffer[nextOffset] == 0 && buffer[nextOffset+1] == 0)
  900. return (nextOffset + sizeOfUChar) - readOffset;
  901. }
  902. }
  903. }
  904. void CThorContiguousRowBuffer::reportReadFail()
  905. {
  906. throwUnexpected();
  907. }
  908. const byte * CThorContiguousRowBuffer::peek(size32_t maxSize)
  909. {
  910. if (maxSize+readOffset > maxOffset)
  911. doPeek(maxSize+readOffset);
  912. return buffer + readOffset;
  913. }
  914. offset_t CThorContiguousRowBuffer::beginNested()
  915. {
  916. size32_t len = readSize();
  917. return len+readOffset;
  918. }
  919. bool CThorContiguousRowBuffer::finishedNested(offset_t endPos)
  920. {
  921. return readOffset >= endPos;
  922. }
  923. void CThorContiguousRowBuffer::skip(size32_t size)
  924. {
  925. ensureAccessible(readOffset+size);
  926. readOffset += size;
  927. }
  928. void CThorContiguousRowBuffer::skipPackedInt()
  929. {
  930. size32_t size = sizePackedInt();
  931. ensureAccessible(readOffset+size);
  932. readOffset += size;
  933. }
  934. void CThorContiguousRowBuffer::skipUtf8(size32_t len)
  935. {
  936. size32_t size = sizeUtf8(len);
  937. ensureAccessible(readOffset+size);
  938. readOffset += size;
  939. }
  940. void CThorContiguousRowBuffer::skipVStr()
  941. {
  942. size32_t size = sizeVStr();
  943. ensureAccessible(readOffset+size);
  944. readOffset += size;
  945. }
  946. void CThorContiguousRowBuffer::skipVUni()
  947. {
  948. size32_t size = sizeVUni();
  949. ensureAccessible(readOffset+size);
  950. readOffset += size;
  951. }
  952. // ===========================================
  953. IRowInterfaces *createRowInterfaces(IOutputMetaData *meta, unsigned actid, ICodeContext *context)
  954. {
  955. class cRowInterfaces: public CSimpleInterface, implements IRowInterfaces
  956. {
  957. Linked<IOutputMetaData> meta;
  958. ICodeContext* context;
  959. unsigned actid;
  960. Linked<IEngineRowAllocator> allocator;
  961. Linked<IOutputRowSerializer> serializer;
  962. Linked<IOutputRowDeserializer> deserializer;
  963. CSingletonLock allocatorlock;
  964. CSingletonLock serializerlock;
  965. CSingletonLock deserializerlock;
  966. public:
  967. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  968. cRowInterfaces(IOutputMetaData *_meta,unsigned _actid, ICodeContext *_context)
  969. : meta(_meta)
  970. {
  971. context = _context;
  972. actid = _actid;
  973. }
  974. IEngineRowAllocator * queryRowAllocator()
  975. {
  976. if (allocatorlock.lock()) {
  977. if (!allocator&&meta)
  978. allocator.setown(context->getRowAllocator(meta, actid));
  979. allocatorlock.unlock();
  980. }
  981. return allocator;
  982. }
  983. IOutputRowSerializer * queryRowSerializer()
  984. {
  985. if (serializerlock.lock()) {
  986. if (!serializer&&meta)
  987. serializer.setown(meta->createRowSerializer(context,actid));
  988. serializerlock.unlock();
  989. }
  990. return serializer;
  991. }
  992. IOutputRowDeserializer * queryRowDeserializer()
  993. {
  994. if (deserializerlock.lock()) {
  995. if (!deserializer&&meta)
  996. deserializer.setown(meta->createRowDeserializer(context,actid));
  997. deserializerlock.unlock();
  998. }
  999. return deserializer;
  1000. }
  1001. IOutputMetaData *queryRowMetaData()
  1002. {
  1003. return meta;
  1004. }
  1005. unsigned queryActivityId()
  1006. {
  1007. return actid;
  1008. }
  1009. ICodeContext *queryCodeContext()
  1010. {
  1011. return context;
  1012. }
  1013. };
  1014. return new cRowInterfaces(meta,actid,context);
  1015. };
  1016. class CRowStreamReader : public CSimpleInterface, implements IExtRowStream
  1017. {
  1018. Linked<IFileIO> fileio;
  1019. Linked<IMemoryMappedFile> mmfile;
  1020. Linked<IOutputRowDeserializer> deserializer;
  1021. Linked<IEngineRowAllocator> allocator;
  1022. Owned<ISerialStream> strm;
  1023. CThorStreamDeserializerSource source;
  1024. Owned<ISourceRowPrefetcher> prefetcher;
  1025. CThorContiguousRowBuffer prefetchBuffer; // used if prefetcher set
  1026. bool grouped;
  1027. unsigned __int64 maxrows;
  1028. unsigned __int64 rownum;
  1029. bool eoi;
  1030. bool eos;
  1031. bool eog;
  1032. offset_t bufofs;
  1033. #ifdef TRACE_CREATE
  1034. static unsigned rdnum;
  1035. #endif
  1036. class : implements IFileSerialStreamCallback
  1037. {
  1038. public:
  1039. CRC32 crc;
  1040. void process(offset_t ofs, size32_t sz, const void *buf)
  1041. {
  1042. crc.tally(sz,buf);
  1043. }
  1044. } crccb;
  1045. public:
  1046. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  1047. CRowStreamReader(IFileIO *_fileio,IMemoryMappedFile *_mmfile,offset_t _ofs, offset_t _len, IRowInterfaces *rowif,unsigned __int64 _maxrows,bool _tallycrc, bool _grouped)
  1048. : fileio(_fileio), mmfile(_mmfile), allocator(rowif->queryRowAllocator()), prefetchBuffer(NULL)
  1049. {
  1050. #ifdef TRACE_CREATE
  1051. PROGLOG("CRowStreamReader %d = %p",++rdnum,this);
  1052. #endif
  1053. maxrows = _maxrows;
  1054. grouped = _grouped;
  1055. eoi = false;
  1056. eos = maxrows==0;
  1057. eog = false;
  1058. bufofs = 0;
  1059. rownum = 0;
  1060. if (fileio)
  1061. strm.setown(createFileSerialStream(fileio,_ofs,_len,(size32_t)-1, _tallycrc?&crccb:NULL));
  1062. else
  1063. strm.setown(createFileSerialStream(mmfile,_ofs,_len,_tallycrc?&crccb:NULL));
  1064. prefetcher.setown(rowif->queryRowMetaData()->createRowPrefetcher(rowif->queryCodeContext(), rowif->queryActivityId()));
  1065. if (prefetcher)
  1066. prefetchBuffer.setStream(strm);
  1067. source.setStream(strm);
  1068. deserializer.set(rowif->queryRowDeserializer());
  1069. }
  1070. ~CRowStreamReader()
  1071. {
  1072. #ifdef TRACE_CREATE
  1073. PROGLOG("~CRowStreamReader %d = %p",rdnum--,this);
  1074. #endif
  1075. }
  1076. void reinit(offset_t _ofs,offset_t _len,unsigned __int64 _maxrows)
  1077. {
  1078. maxrows = _maxrows;
  1079. eoi = false;
  1080. eos = (maxrows==0)||(_len==0);
  1081. eog = false;
  1082. bufofs = 0;
  1083. rownum = 0;
  1084. strm->reset(_ofs,_len);
  1085. }
  1086. const void *nextRow()
  1087. {
  1088. if (eog) {
  1089. eog = false;
  1090. return NULL;
  1091. }
  1092. if (eos)
  1093. return NULL;
  1094. if (source.eos()) {
  1095. eos = true;
  1096. return NULL;
  1097. }
  1098. RtlDynamicRowBuilder rowBuilder(allocator);
  1099. size_t size = deserializer->deserialize(rowBuilder,source);
  1100. if (grouped && !eos) {
  1101. byte b;
  1102. source.read(sizeof(b),&b);
  1103. eog = (b==1);
  1104. }
  1105. if (++rownum==maxrows)
  1106. eos = true;
  1107. return rowBuilder.finalizeRowClear(size);
  1108. }
  1109. const void *prefetchRow(size32_t *sz)
  1110. {
  1111. if (eog)
  1112. eog = false;
  1113. else if (!eos) {
  1114. if (source.eos())
  1115. eos = true;
  1116. else {
  1117. assertex(prefetcher);
  1118. prefetcher->readAhead(prefetchBuffer);
  1119. const byte * ret = prefetchBuffer.queryRow();
  1120. if (sz)
  1121. *sz = prefetchBuffer.queryRowSize();
  1122. return ret;
  1123. }
  1124. }
  1125. if (sz)
  1126. sz = 0;
  1127. return NULL;
  1128. }
  1129. void prefetchDone()
  1130. {
  1131. prefetchBuffer.finishedRow();
  1132. if (grouped) {
  1133. byte b;
  1134. strm->get(sizeof(b),&b);
  1135. eog = (b==1);
  1136. }
  1137. }
  1138. virtual void stop()
  1139. {
  1140. stop(NULL);
  1141. }
  1142. void clear()
  1143. {
  1144. strm.clear();
  1145. source.clearStream();
  1146. fileio.clear();
  1147. }
  1148. void stop(CRC32 *crcout)
  1149. {
  1150. if (!eos) {
  1151. eos = true;
  1152. clear();
  1153. }
  1154. // NB CRC will only be right if stopped at eos
  1155. if (crcout)
  1156. *crcout = crccb.crc;
  1157. }
  1158. offset_t getOffset()
  1159. {
  1160. return source.tell();
  1161. }
  1162. };
  1163. #ifdef TRACE_CREATE
  1164. unsigned CRowStreamReader::rdnum;
  1165. #endif
  1166. bool UseMemoryMappedRead = false;
  1167. IExtRowStream *createRowStream(IFile *file,IRowInterfaces *rowif,offset_t offset,offset_t len,unsigned __int64 maxrows,bool tallycrc,bool grouped)
  1168. {
  1169. IExtRowStream *ret;
  1170. if (UseMemoryMappedRead) {
  1171. PROGLOG("Memory Mapped read of %s",file->queryFilename());
  1172. Owned<IMemoryMappedFile> mmfile = file->openMemoryMapped();
  1173. if (!mmfile)
  1174. return NULL;
  1175. ret = new CRowStreamReader(NULL,mmfile,offset,len,rowif,maxrows,tallycrc,grouped);
  1176. }
  1177. else {
  1178. Owned<IFileIO> fileio = file->open(IFOread);
  1179. if (!fileio)
  1180. return NULL;
  1181. ret = new CRowStreamReader(fileio,NULL,offset,len,rowif,maxrows,tallycrc,grouped);
  1182. }
  1183. return ret;
  1184. }
  1185. IExtRowStream *createCompressedRowStream(IFile *file,IRowInterfaces *rowif,offset_t offset,offset_t len,unsigned __int64 maxrows,bool tallycrc,bool grouped,IExpander *eexp)
  1186. {
  1187. Owned<IFileIO> fileio = createCompressedFileReader(file, eexp, UseMemoryMappedRead);
  1188. if (!fileio)
  1189. return NULL;
  1190. IExtRowStream *ret = new CRowStreamReader(fileio,NULL,offset,len,rowif,maxrows,tallycrc,grouped);
  1191. return ret;
  1192. }
  1193. void useMemoryMappedRead(bool on)
  1194. {
  1195. #if defined(_DEBUG) || defined(__64BIT__)
  1196. UseMemoryMappedRead = on;
  1197. #endif
  1198. }
  1199. #define ROW_WRITER_BUFFERSIZE (0x100000)
  1200. class CRowStreamWriter : public CSimpleInterface, private IRowSerializerTarget, implements IExtRowWriter
  1201. {
  1202. Linked<IFileIOStream> stream;
  1203. Linked<IOutputRowSerializer> serializer;
  1204. Linked<IEngineRowAllocator> allocator;
  1205. CRC32 crc;
  1206. bool grouped;
  1207. bool tallycrc;
  1208. unsigned nested;
  1209. MemoryAttr ma;
  1210. MemoryBuffer extbuf; // may need to spill to disk at some point
  1211. byte *buf;
  1212. size32_t bufpos;
  1213. bool autoflush;
  1214. #ifdef TRACE_CREATE
  1215. static unsigned wrnum;
  1216. #endif
  1217. void flushBuffer(bool final)
  1218. {
  1219. if (bufpos) {
  1220. stream->write(bufpos,buf);
  1221. if (tallycrc)
  1222. crc.tally(bufpos,buf);
  1223. bufpos = 0;
  1224. }
  1225. size32_t extpos = extbuf.length();
  1226. if (!extpos)
  1227. return;
  1228. if (!final)
  1229. extpos = (extpos/ROW_WRITER_BUFFERSIZE)*ROW_WRITER_BUFFERSIZE;
  1230. if (extpos) {
  1231. stream->write(extpos,extbuf.toByteArray());
  1232. if (tallycrc)
  1233. crc.tally(extpos,extbuf.toByteArray());
  1234. }
  1235. if (extpos<extbuf.length()) {
  1236. bufpos = extbuf.length()-extpos;
  1237. memcpy(buf,extbuf.toByteArray()+extpos,bufpos);
  1238. }
  1239. extbuf.clear();
  1240. }
  1241. public:
  1242. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  1243. CRowStreamWriter(IFileIOStream *_stream,IOutputRowSerializer *_serializer,IEngineRowAllocator *_allocator,bool _grouped, bool _tallycrc, bool _autoflush)
  1244. : stream(_stream), serializer(_serializer), allocator(_allocator)
  1245. {
  1246. #ifdef TRACE_CREATE
  1247. PROGLOG("createRowWriter %d = %p",++wrnum,this);
  1248. #endif
  1249. grouped = _grouped;
  1250. tallycrc = _tallycrc;
  1251. nested = 0;
  1252. buf = (byte *)ma.allocate(ROW_WRITER_BUFFERSIZE);
  1253. bufpos = 0;
  1254. autoflush = _autoflush;
  1255. }
  1256. ~CRowStreamWriter()
  1257. {
  1258. #ifdef TRACE_CREATE
  1259. PROGLOG("~createRowWriter %d = %p",wrnum--,this);
  1260. #endif
  1261. if (autoflush)
  1262. flush();
  1263. else if (bufpos+extbuf.length()) {
  1264. #ifdef _DEBUG
  1265. PrintStackReport();
  1266. #endif
  1267. WARNLOG("CRowStreamWriter closed with %d bytes unflushed",bufpos+extbuf.length());
  1268. }
  1269. }
  1270. void putRow(const void *row)
  1271. {
  1272. if (row) {
  1273. serializer->serialize(*this,(const byte *)row);
  1274. if (grouped) {
  1275. byte b = 0;
  1276. if (bufpos<ROW_WRITER_BUFFERSIZE)
  1277. buf[bufpos++] = b;
  1278. else
  1279. extbuf.append(b);
  1280. }
  1281. allocator->releaseRow(row);
  1282. }
  1283. else if (grouped) { // backpatch
  1284. byte b = 1;
  1285. if (extbuf.length())
  1286. extbuf.writeDirect(extbuf.length()-1,sizeof(b),&b);
  1287. else {
  1288. assertex(bufpos);
  1289. buf[bufpos-1] = b;
  1290. }
  1291. }
  1292. }
  1293. void flush()
  1294. {
  1295. flushBuffer(true);
  1296. stream->flush();
  1297. }
  1298. void flush(CRC32 *crcout)
  1299. {
  1300. flushBuffer(true);
  1301. stream->flush();
  1302. if (crcout)
  1303. *crcout = crc;
  1304. }
  1305. offset_t getPosition()
  1306. {
  1307. return stream->tell()+bufpos+extbuf.length();
  1308. }
  1309. void put(size32_t len, const void * ptr)
  1310. {
  1311. // first fill buf
  1312. loop {
  1313. if (bufpos<ROW_WRITER_BUFFERSIZE) {
  1314. size32_t wr = ROW_WRITER_BUFFERSIZE-bufpos;
  1315. if (wr>len)
  1316. wr = len;
  1317. memcpy(buf+bufpos,ptr,wr);
  1318. bufpos += wr;
  1319. len -= wr;
  1320. if (len==0)
  1321. break; // quick exit
  1322. ptr = (const byte *)ptr + wr;
  1323. }
  1324. if (nested) {
  1325. // have to append to ext buffer (will need to spill to disk here if gets *too* big)
  1326. extbuf.append(len,ptr);
  1327. break;
  1328. }
  1329. else
  1330. flushBuffer(false);
  1331. }
  1332. }
  1333. size32_t beginNested()
  1334. {
  1335. if (nested++==0)
  1336. if (bufpos==ROW_WRITER_BUFFERSIZE)
  1337. flushBuffer(false);
  1338. size32_t ret = bufpos+extbuf.length();
  1339. size32_t sz = 0;
  1340. put(sizeof(sz),&sz);
  1341. return ret;
  1342. }
  1343. void endNested(size32_t pos)
  1344. {
  1345. size32_t sz = bufpos+extbuf.length()-(pos + sizeof(size32_t));
  1346. size32_t wr = sizeof(size32_t);
  1347. byte *out = (byte *)&sz;
  1348. if (pos<ROW_WRITER_BUFFERSIZE) {
  1349. size32_t space = ROW_WRITER_BUFFERSIZE-pos;
  1350. if (space>wr)
  1351. space = wr;
  1352. memcpy(buf+pos,out,space);
  1353. wr -= space;
  1354. if (wr==0) {
  1355. --nested;
  1356. return; // quick exit
  1357. }
  1358. out += space;
  1359. pos += space;
  1360. }
  1361. extbuf.writeDirect(pos-ROW_WRITER_BUFFERSIZE,wr,out);
  1362. --nested;
  1363. }
  1364. };
  1365. #ifdef TRACE_CREATE
  1366. unsigned CRowStreamWriter::wrnum=0;
  1367. #endif
  1368. IExtRowWriter *createRowWriter(IFile *file,IOutputRowSerializer *serializer,IEngineRowAllocator *allocator,bool grouped, bool tallycrc, bool extend)
  1369. {
  1370. Owned<IFileIO> fileio = file->open(extend?IFOwrite:IFOcreate);
  1371. if (!fileio)
  1372. return NULL;
  1373. Owned<IFileIOStream> stream = createIOStream(fileio);
  1374. if (extend)
  1375. stream->seek(0,IFSend);
  1376. return createRowWriter(stream,serializer,allocator,grouped,tallycrc,true);
  1377. }
  1378. IExtRowWriter *createRowWriter(IFileIOStream *strm,IOutputRowSerializer *serializer,IEngineRowAllocator *allocator,bool grouped, bool tallycrc, bool autoflush)
  1379. {
  1380. Owned<CRowStreamWriter> writer = new CRowStreamWriter(strm, serializer, allocator, grouped, tallycrc, autoflush);
  1381. return writer.getClear();
  1382. }
  1383. class CDiskMerger : public CInterface, implements IDiskMerger
  1384. {
  1385. IArrayOf<IFile> tempfiles;
  1386. IRowStream **strms;
  1387. Linked<IRecordSize> irecsize;
  1388. StringAttr tempnamebase;
  1389. Linked<IRowLinkCounter> linker;
  1390. Linked<IRowInterfaces> rowInterfaces;
  1391. public:
  1392. IMPLEMENT_IINTERFACE;
  1393. CDiskMerger(IRowInterfaces *_rowInterfaces, IRowLinkCounter *_linker, const char *_tempnamebase)
  1394. : rowInterfaces(_rowInterfaces), linker(_linker), tempnamebase(_tempnamebase)
  1395. {
  1396. strms = NULL;
  1397. }
  1398. ~CDiskMerger()
  1399. {
  1400. for (unsigned i=0;i<tempfiles.ordinality();i++) {
  1401. if (strms&&strms[i])
  1402. strms[i]->Release();
  1403. tempfiles.item(i).remove();
  1404. }
  1405. free(strms);
  1406. }
  1407. IRowWriter *createWriteBlock()
  1408. {
  1409. StringBuffer tempname(tempnamebase);
  1410. tempname.append('.').append(tempfiles.ordinality()).append('_').append((__int64)GetCurrentThreadId()).append('_').append((unsigned)GetCurrentProcessId());
  1411. IFile *file = createIFile(tempname.str());
  1412. tempfiles.append(*file);
  1413. return createRowWriter(file,rowInterfaces->queryRowSerializer(),rowInterfaces->queryRowAllocator(),false,false,false); // flushed by close
  1414. }
  1415. void put(const void **rows,unsigned numrows)
  1416. {
  1417. Owned<IRowWriter> out = createWriteBlock();
  1418. for (unsigned i=0;i<numrows;i++)
  1419. out->putRow(rows[i]);
  1420. }
  1421. void putIndirect(const void ***rowptrs,unsigned numrows)
  1422. {
  1423. Owned<IRowWriter> out = createWriteBlock();
  1424. for (unsigned i=0;i<numrows;i++)
  1425. out->putRow(*(rowptrs[i]));
  1426. }
  1427. virtual void put(ISortedRowProvider *rows)
  1428. {
  1429. Owned<IRowWriter> out = createWriteBlock();
  1430. void * row;
  1431. while(row = rows->getNextSorted())
  1432. out->putRow(row);
  1433. }
  1434. IRowStream *merge(ICompare *icompare, bool partdedup)
  1435. {
  1436. unsigned numstrms = tempfiles.ordinality();
  1437. strms = (IRowStream **)calloc(numstrms,sizeof(IRowStream *));
  1438. unsigned i;
  1439. for (i=0;i<numstrms;i++) {
  1440. strms[i] = createSimpleRowStream(&tempfiles.item(i), rowInterfaces);
  1441. }
  1442. if (numstrms==1)
  1443. return LINK(strms[0]);
  1444. if (icompare)
  1445. return createRowStreamMerger(numstrms, strms, icompare, partdedup, linker);
  1446. return createConcatRowStream(numstrms,strms);
  1447. }
  1448. virtual count_t mergeTo(IRowWriter *dest, ICompare *icompare, bool partdedup)
  1449. {
  1450. count_t count = 0;
  1451. Owned<IRowStream> mergedStream = merge(icompare, partdedup);
  1452. loop
  1453. {
  1454. const void *row = mergedStream->nextRow();
  1455. if (!row)
  1456. return count;
  1457. dest->putRow(row); // takes ownership
  1458. ++count;
  1459. }
  1460. return count;
  1461. }
  1462. };
  1463. IDiskMerger *createDiskMerger(IRowInterfaces *rowInterfaces, IRowLinkCounter *linker, const char *tempnamebase)
  1464. {
  1465. return new CDiskMerger(rowInterfaces, linker, tempnamebase);
  1466. }