thorcommon.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. case TAKshuffle: return "Shuffle";
  712. }
  713. throwUnexpected();
  714. }
  715. extern bool isActivitySource(ThorActivityKind kind)
  716. {
  717. switch (kind)
  718. {
  719. case TAKpiperead:
  720. case TAKtemptable:
  721. case TAKtemprow:
  722. case TAKworkunitread:
  723. case TAKnull:
  724. case TAKsideeffect:
  725. case TAKsoap_rowdataset:
  726. case TAKsoap_rowaction:
  727. case TAKkeydiff:
  728. case TAKkeypatch:
  729. case TAKchilditerator:
  730. case TAKlocalgraph:
  731. case TAKemptyaction:
  732. case TAKdiskread:
  733. case TAKdisknormalize:
  734. case TAKdiskaggregate:
  735. case TAKdiskcount:
  736. case TAKdiskgroupaggregate:
  737. case TAKindexread:
  738. case TAKindexnormalize:
  739. case TAKindexaggregate:
  740. case TAKindexcount:
  741. case TAKindexgroupaggregate:
  742. case TAKchildnormalize:
  743. case TAKchildaggregate:
  744. case TAKchildgroupaggregate:
  745. case TAKcsvread:
  746. case TAKxmlread:
  747. case TAKlocalresultread:
  748. case TAKsimpleaction:
  749. case TAKrawiterator:
  750. case TAKlocalstreamread:
  751. case TAKgraphloopresultread:
  752. case TAKnwaygraphloopresultread:
  753. case TAKlinkedrawiterator:
  754. case TAKindexgroupexists:
  755. case TAKindexgroupcount:
  756. case TAKstreamediterator:
  757. case TAKexternalsource:
  758. return true;
  759. }
  760. return false;
  761. }
  762. extern bool isActivitySink(ThorActivityKind kind)
  763. {
  764. switch (kind)
  765. {
  766. case TAKdiskwrite:
  767. case TAKworkunitwrite:
  768. case TAKapply:
  769. case TAKremoteresult:
  770. case TAKdistribution:
  771. case TAKpipewrite:
  772. case TAKcsvwrite:
  773. case TAKindexwrite:
  774. case TAKxmlwrite:
  775. case TAKsoap_rowaction:
  776. case TAKsoap_datasetaction:
  777. case TAKkeydiff:
  778. case TAKkeypatch:
  779. case TAKdatasetresult:
  780. case TAKrowresult:
  781. case TAKemptyaction:
  782. case TAKlocalresultwrite:
  783. case TAKgraphloopresultwrite:
  784. case TAKsimpleaction:
  785. case TAKexternalsink:
  786. case TAKifaction:
  787. case TAKparallel:
  788. case TAKsequential:
  789. case TAKwhen_action:
  790. return true;
  791. }
  792. return false;
  793. }
  794. //------------------------------------------------------------------------------------------------
  795. byte * CStaticRowBuilder::ensureCapacity(size32_t required, const char * fieldName)
  796. {
  797. if (required <= maxLength)
  798. return static_cast<byte *>(self);
  799. rtlReportFieldOverflow(required, maxLength, fieldName);
  800. return NULL;
  801. }
  802. //=====================================================================================================
  803. CThorContiguousRowBuffer::CThorContiguousRowBuffer(ISerialStream * _in) : in(_in)
  804. {
  805. buffer = NULL;
  806. maxOffset = 0;
  807. readOffset = 0;
  808. }
  809. void CThorContiguousRowBuffer::doRead(size32_t len, void * ptr)
  810. {
  811. ensureAccessible(readOffset + len);
  812. memcpy(ptr, buffer+readOffset, len);
  813. readOffset += len;
  814. }
  815. size32_t CThorContiguousRowBuffer::read(size32_t len, void * ptr)
  816. {
  817. doRead(len, ptr);
  818. return len;
  819. }
  820. size32_t CThorContiguousRowBuffer::readSize()
  821. {
  822. size32_t value;
  823. doRead(sizeof(value), &value);
  824. return value;
  825. }
  826. size32_t CThorContiguousRowBuffer::readPackedInt(void * ptr)
  827. {
  828. size32_t size = sizePackedInt();
  829. doRead(size, ptr);
  830. return size;
  831. }
  832. size32_t CThorContiguousRowBuffer::readUtf8(ARowBuilder & target, size32_t offset, size32_t fixedSize, size32_t len)
  833. {
  834. if (len == 0)
  835. return 0;
  836. size32_t size = sizeUtf8(len);
  837. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  838. doRead(size, self+offset);
  839. return size;
  840. }
  841. size32_t CThorContiguousRowBuffer::readVStr(ARowBuilder & target, size32_t offset, size32_t fixedSize)
  842. {
  843. size32_t size = sizeVStr();
  844. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  845. doRead(size, self+offset);
  846. return size;
  847. }
  848. size32_t CThorContiguousRowBuffer::readVUni(ARowBuilder & target, size32_t offset, size32_t fixedSize)
  849. {
  850. size32_t size = sizeVUni();
  851. byte * self = target.ensureCapacity(fixedSize + size, NULL);
  852. doRead(size, self+offset);
  853. return size;
  854. }
  855. size32_t CThorContiguousRowBuffer::sizePackedInt()
  856. {
  857. ensureAccessible(readOffset+1);
  858. return rtlGetPackedSizeFromFirst(buffer[readOffset]);
  859. }
  860. size32_t CThorContiguousRowBuffer::sizeUtf8(size32_t len)
  861. {
  862. if (len == 0)
  863. return 0;
  864. //The len is the number of utf characters, size depends on which characters are included.
  865. size32_t nextOffset = readOffset;
  866. while (len)
  867. {
  868. ensureAccessible(nextOffset+1);
  869. for (;nextOffset < maxOffset;)
  870. {
  871. nextOffset += readUtf8Size(buffer+nextOffset); // This function only accesses the first byte
  872. if (--len == 0)
  873. break;
  874. }
  875. }
  876. return nextOffset - readOffset;
  877. }
  878. size32_t CThorContiguousRowBuffer::sizeVStr()
  879. {
  880. size32_t nextOffset = readOffset;
  881. loop
  882. {
  883. ensureAccessible(nextOffset+1);
  884. for (; nextOffset < maxOffset; nextOffset++)
  885. {
  886. if (buffer[nextOffset] == 0)
  887. return (nextOffset + 1) - readOffset;
  888. }
  889. }
  890. }
  891. size32_t CThorContiguousRowBuffer::sizeVUni()
  892. {
  893. size32_t nextOffset = readOffset;
  894. const size32_t sizeOfUChar = 2;
  895. loop
  896. {
  897. ensureAccessible(nextOffset+sizeOfUChar);
  898. for (; nextOffset+1 < maxOffset; nextOffset += sizeOfUChar)
  899. {
  900. if (buffer[nextOffset] == 0 && buffer[nextOffset+1] == 0)
  901. return (nextOffset + sizeOfUChar) - readOffset;
  902. }
  903. }
  904. }
  905. void CThorContiguousRowBuffer::reportReadFail()
  906. {
  907. throwUnexpected();
  908. }
  909. const byte * CThorContiguousRowBuffer::peek(size32_t maxSize)
  910. {
  911. if (maxSize+readOffset > maxOffset)
  912. doPeek(maxSize+readOffset);
  913. return buffer + readOffset;
  914. }
  915. offset_t CThorContiguousRowBuffer::beginNested()
  916. {
  917. size32_t len = readSize();
  918. return len+readOffset;
  919. }
  920. bool CThorContiguousRowBuffer::finishedNested(offset_t endPos)
  921. {
  922. return readOffset >= endPos;
  923. }
  924. void CThorContiguousRowBuffer::skip(size32_t size)
  925. {
  926. ensureAccessible(readOffset+size);
  927. readOffset += size;
  928. }
  929. void CThorContiguousRowBuffer::skipPackedInt()
  930. {
  931. size32_t size = sizePackedInt();
  932. ensureAccessible(readOffset+size);
  933. readOffset += size;
  934. }
  935. void CThorContiguousRowBuffer::skipUtf8(size32_t len)
  936. {
  937. size32_t size = sizeUtf8(len);
  938. ensureAccessible(readOffset+size);
  939. readOffset += size;
  940. }
  941. void CThorContiguousRowBuffer::skipVStr()
  942. {
  943. size32_t size = sizeVStr();
  944. ensureAccessible(readOffset+size);
  945. readOffset += size;
  946. }
  947. void CThorContiguousRowBuffer::skipVUni()
  948. {
  949. size32_t size = sizeVUni();
  950. ensureAccessible(readOffset+size);
  951. readOffset += size;
  952. }
  953. // ===========================================
  954. IRowInterfaces *createRowInterfaces(IOutputMetaData *meta, unsigned actid, ICodeContext *context)
  955. {
  956. class cRowInterfaces: public CSimpleInterface, implements IRowInterfaces
  957. {
  958. Linked<IOutputMetaData> meta;
  959. ICodeContext* context;
  960. unsigned actid;
  961. Linked<IEngineRowAllocator> allocator;
  962. Linked<IOutputRowSerializer> serializer;
  963. Linked<IOutputRowDeserializer> deserializer;
  964. CSingletonLock allocatorlock;
  965. CSingletonLock serializerlock;
  966. CSingletonLock deserializerlock;
  967. public:
  968. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  969. cRowInterfaces(IOutputMetaData *_meta,unsigned _actid, ICodeContext *_context)
  970. : meta(_meta)
  971. {
  972. context = _context;
  973. actid = _actid;
  974. }
  975. IEngineRowAllocator * queryRowAllocator()
  976. {
  977. if (allocatorlock.lock()) {
  978. if (!allocator&&meta)
  979. allocator.setown(context->getRowAllocator(meta, actid));
  980. allocatorlock.unlock();
  981. }
  982. return allocator;
  983. }
  984. IOutputRowSerializer * queryRowSerializer()
  985. {
  986. if (serializerlock.lock()) {
  987. if (!serializer&&meta)
  988. serializer.setown(meta->createRowSerializer(context,actid));
  989. serializerlock.unlock();
  990. }
  991. return serializer;
  992. }
  993. IOutputRowDeserializer * queryRowDeserializer()
  994. {
  995. if (deserializerlock.lock()) {
  996. if (!deserializer&&meta)
  997. deserializer.setown(meta->createRowDeserializer(context,actid));
  998. deserializerlock.unlock();
  999. }
  1000. return deserializer;
  1001. }
  1002. IOutputMetaData *queryRowMetaData()
  1003. {
  1004. return meta;
  1005. }
  1006. unsigned queryActivityId()
  1007. {
  1008. return actid;
  1009. }
  1010. ICodeContext *queryCodeContext()
  1011. {
  1012. return context;
  1013. }
  1014. };
  1015. return new cRowInterfaces(meta,actid,context);
  1016. };
  1017. class CRowStreamReader : public CSimpleInterface, implements IExtRowStream
  1018. {
  1019. Linked<IFileIO> fileio;
  1020. Linked<IMemoryMappedFile> mmfile;
  1021. Linked<IOutputRowDeserializer> deserializer;
  1022. Linked<IEngineRowAllocator> allocator;
  1023. Owned<ISerialStream> strm;
  1024. CThorStreamDeserializerSource source;
  1025. Owned<ISourceRowPrefetcher> prefetcher;
  1026. CThorContiguousRowBuffer prefetchBuffer; // used if prefetcher set
  1027. bool grouped;
  1028. unsigned __int64 maxrows;
  1029. unsigned __int64 rownum;
  1030. bool eoi;
  1031. bool eos;
  1032. bool eog;
  1033. offset_t bufofs;
  1034. #ifdef TRACE_CREATE
  1035. static unsigned rdnum;
  1036. #endif
  1037. class : implements IFileSerialStreamCallback
  1038. {
  1039. public:
  1040. CRC32 crc;
  1041. void process(offset_t ofs, size32_t sz, const void *buf)
  1042. {
  1043. crc.tally(sz,buf);
  1044. }
  1045. } crccb;
  1046. public:
  1047. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  1048. CRowStreamReader(IFileIO *_fileio,IMemoryMappedFile *_mmfile,offset_t _ofs, offset_t _len, IRowInterfaces *rowif,unsigned __int64 _maxrows,bool _tallycrc, bool _grouped)
  1049. : fileio(_fileio), mmfile(_mmfile), allocator(rowif->queryRowAllocator()), prefetchBuffer(NULL)
  1050. {
  1051. #ifdef TRACE_CREATE
  1052. PROGLOG("CRowStreamReader %d = %p",++rdnum,this);
  1053. #endif
  1054. maxrows = _maxrows;
  1055. grouped = _grouped;
  1056. eoi = false;
  1057. eos = maxrows==0;
  1058. eog = false;
  1059. bufofs = 0;
  1060. rownum = 0;
  1061. if (fileio)
  1062. strm.setown(createFileSerialStream(fileio,_ofs,_len,(size32_t)-1, _tallycrc?&crccb:NULL));
  1063. else
  1064. strm.setown(createFileSerialStream(mmfile,_ofs,_len,_tallycrc?&crccb:NULL));
  1065. prefetcher.setown(rowif->queryRowMetaData()->createRowPrefetcher(rowif->queryCodeContext(), rowif->queryActivityId()));
  1066. if (prefetcher)
  1067. prefetchBuffer.setStream(strm);
  1068. source.setStream(strm);
  1069. deserializer.set(rowif->queryRowDeserializer());
  1070. }
  1071. ~CRowStreamReader()
  1072. {
  1073. #ifdef TRACE_CREATE
  1074. PROGLOG("~CRowStreamReader %d = %p",rdnum--,this);
  1075. #endif
  1076. }
  1077. void reinit(offset_t _ofs,offset_t _len,unsigned __int64 _maxrows)
  1078. {
  1079. maxrows = _maxrows;
  1080. eoi = false;
  1081. eos = (maxrows==0)||(_len==0);
  1082. eog = false;
  1083. bufofs = 0;
  1084. rownum = 0;
  1085. strm->reset(_ofs,_len);
  1086. }
  1087. const void *nextRow()
  1088. {
  1089. if (eog) {
  1090. eog = false;
  1091. return NULL;
  1092. }
  1093. if (eos)
  1094. return NULL;
  1095. if (source.eos()) {
  1096. eos = true;
  1097. return NULL;
  1098. }
  1099. RtlDynamicRowBuilder rowBuilder(allocator);
  1100. size_t size = deserializer->deserialize(rowBuilder,source);
  1101. if (grouped && !eos) {
  1102. byte b;
  1103. source.read(sizeof(b),&b);
  1104. eog = (b==1);
  1105. }
  1106. if (++rownum==maxrows)
  1107. eos = true;
  1108. return rowBuilder.finalizeRowClear(size);
  1109. }
  1110. const void *prefetchRow(size32_t *sz)
  1111. {
  1112. if (eog)
  1113. eog = false;
  1114. else if (!eos) {
  1115. if (source.eos())
  1116. eos = true;
  1117. else {
  1118. assertex(prefetcher);
  1119. prefetcher->readAhead(prefetchBuffer);
  1120. const byte * ret = prefetchBuffer.queryRow();
  1121. if (sz)
  1122. *sz = prefetchBuffer.queryRowSize();
  1123. return ret;
  1124. }
  1125. }
  1126. if (sz)
  1127. sz = 0;
  1128. return NULL;
  1129. }
  1130. void prefetchDone()
  1131. {
  1132. prefetchBuffer.finishedRow();
  1133. if (grouped) {
  1134. byte b;
  1135. strm->get(sizeof(b),&b);
  1136. eog = (b==1);
  1137. }
  1138. }
  1139. virtual void stop()
  1140. {
  1141. stop(NULL);
  1142. }
  1143. void clear()
  1144. {
  1145. strm.clear();
  1146. source.clearStream();
  1147. fileio.clear();
  1148. }
  1149. void stop(CRC32 *crcout)
  1150. {
  1151. if (!eos) {
  1152. eos = true;
  1153. clear();
  1154. }
  1155. // NB CRC will only be right if stopped at eos
  1156. if (crcout)
  1157. *crcout = crccb.crc;
  1158. }
  1159. offset_t getOffset()
  1160. {
  1161. return source.tell();
  1162. }
  1163. };
  1164. #ifdef TRACE_CREATE
  1165. unsigned CRowStreamReader::rdnum;
  1166. #endif
  1167. bool UseMemoryMappedRead = false;
  1168. IExtRowStream *createRowStream(IFile *file,IRowInterfaces *rowif,offset_t offset,offset_t len,unsigned __int64 maxrows,bool tallycrc,bool grouped)
  1169. {
  1170. IExtRowStream *ret;
  1171. if (UseMemoryMappedRead) {
  1172. PROGLOG("Memory Mapped read of %s",file->queryFilename());
  1173. Owned<IMemoryMappedFile> mmfile = file->openMemoryMapped();
  1174. if (!mmfile)
  1175. return NULL;
  1176. ret = new CRowStreamReader(NULL,mmfile,offset,len,rowif,maxrows,tallycrc,grouped);
  1177. }
  1178. else {
  1179. Owned<IFileIO> fileio = file->open(IFOread);
  1180. if (!fileio)
  1181. return NULL;
  1182. ret = new CRowStreamReader(fileio,NULL,offset,len,rowif,maxrows,tallycrc,grouped);
  1183. }
  1184. return ret;
  1185. }
  1186. IExtRowStream *createCompressedRowStream(IFile *file,IRowInterfaces *rowif,offset_t offset,offset_t len,unsigned __int64 maxrows,bool tallycrc,bool grouped,IExpander *eexp)
  1187. {
  1188. Owned<IFileIO> fileio = createCompressedFileReader(file, eexp, UseMemoryMappedRead);
  1189. if (!fileio)
  1190. return NULL;
  1191. IExtRowStream *ret = new CRowStreamReader(fileio,NULL,offset,len,rowif,maxrows,tallycrc,grouped);
  1192. return ret;
  1193. }
  1194. void useMemoryMappedRead(bool on)
  1195. {
  1196. #if defined(_DEBUG) || defined(__64BIT__)
  1197. UseMemoryMappedRead = on;
  1198. #endif
  1199. }
  1200. #define ROW_WRITER_BUFFERSIZE (0x100000)
  1201. class CRowStreamWriter : public CSimpleInterface, private IRowSerializerTarget, implements IExtRowWriter
  1202. {
  1203. Linked<IFileIOStream> stream;
  1204. Linked<IOutputRowSerializer> serializer;
  1205. Linked<IEngineRowAllocator> allocator;
  1206. CRC32 crc;
  1207. bool grouped;
  1208. bool tallycrc;
  1209. unsigned nested;
  1210. MemoryAttr ma;
  1211. MemoryBuffer extbuf; // may need to spill to disk at some point
  1212. byte *buf;
  1213. size32_t bufpos;
  1214. bool autoflush;
  1215. #ifdef TRACE_CREATE
  1216. static unsigned wrnum;
  1217. #endif
  1218. void flushBuffer(bool final)
  1219. {
  1220. if (bufpos) {
  1221. stream->write(bufpos,buf);
  1222. if (tallycrc)
  1223. crc.tally(bufpos,buf);
  1224. bufpos = 0;
  1225. }
  1226. size32_t extpos = extbuf.length();
  1227. if (!extpos)
  1228. return;
  1229. if (!final)
  1230. extpos = (extpos/ROW_WRITER_BUFFERSIZE)*ROW_WRITER_BUFFERSIZE;
  1231. if (extpos) {
  1232. stream->write(extpos,extbuf.toByteArray());
  1233. if (tallycrc)
  1234. crc.tally(extpos,extbuf.toByteArray());
  1235. }
  1236. if (extpos<extbuf.length()) {
  1237. bufpos = extbuf.length()-extpos;
  1238. memcpy(buf,extbuf.toByteArray()+extpos,bufpos);
  1239. }
  1240. extbuf.clear();
  1241. }
  1242. public:
  1243. IMPLEMENT_IINTERFACE_USING(CSimpleInterface);
  1244. CRowStreamWriter(IFileIOStream *_stream,IOutputRowSerializer *_serializer,IEngineRowAllocator *_allocator,bool _grouped, bool _tallycrc, bool _autoflush)
  1245. : stream(_stream), serializer(_serializer), allocator(_allocator)
  1246. {
  1247. #ifdef TRACE_CREATE
  1248. PROGLOG("createRowWriter %d = %p",++wrnum,this);
  1249. #endif
  1250. grouped = _grouped;
  1251. tallycrc = _tallycrc;
  1252. nested = 0;
  1253. buf = (byte *)ma.allocate(ROW_WRITER_BUFFERSIZE);
  1254. bufpos = 0;
  1255. autoflush = _autoflush;
  1256. }
  1257. ~CRowStreamWriter()
  1258. {
  1259. #ifdef TRACE_CREATE
  1260. PROGLOG("~createRowWriter %d = %p",wrnum--,this);
  1261. #endif
  1262. if (autoflush)
  1263. flush();
  1264. else if (bufpos+extbuf.length()) {
  1265. #ifdef _DEBUG
  1266. PrintStackReport();
  1267. #endif
  1268. WARNLOG("CRowStreamWriter closed with %d bytes unflushed",bufpos+extbuf.length());
  1269. }
  1270. }
  1271. void putRow(const void *row)
  1272. {
  1273. if (row) {
  1274. serializer->serialize(*this,(const byte *)row);
  1275. if (grouped) {
  1276. byte b = 0;
  1277. if (bufpos<ROW_WRITER_BUFFERSIZE)
  1278. buf[bufpos++] = b;
  1279. else
  1280. extbuf.append(b);
  1281. }
  1282. allocator->releaseRow(row);
  1283. }
  1284. else if (grouped) { // backpatch
  1285. byte b = 1;
  1286. if (extbuf.length())
  1287. extbuf.writeDirect(extbuf.length()-1,sizeof(b),&b);
  1288. else {
  1289. assertex(bufpos);
  1290. buf[bufpos-1] = b;
  1291. }
  1292. }
  1293. }
  1294. void flush()
  1295. {
  1296. flushBuffer(true);
  1297. stream->flush();
  1298. }
  1299. void flush(CRC32 *crcout)
  1300. {
  1301. flushBuffer(true);
  1302. stream->flush();
  1303. if (crcout)
  1304. *crcout = crc;
  1305. }
  1306. offset_t getPosition()
  1307. {
  1308. return stream->tell()+bufpos+extbuf.length();
  1309. }
  1310. void put(size32_t len, const void * ptr)
  1311. {
  1312. // first fill buf
  1313. loop {
  1314. if (bufpos<ROW_WRITER_BUFFERSIZE) {
  1315. size32_t wr = ROW_WRITER_BUFFERSIZE-bufpos;
  1316. if (wr>len)
  1317. wr = len;
  1318. memcpy(buf+bufpos,ptr,wr);
  1319. bufpos += wr;
  1320. len -= wr;
  1321. if (len==0)
  1322. break; // quick exit
  1323. ptr = (const byte *)ptr + wr;
  1324. }
  1325. if (nested) {
  1326. // have to append to ext buffer (will need to spill to disk here if gets *too* big)
  1327. extbuf.append(len,ptr);
  1328. break;
  1329. }
  1330. else
  1331. flushBuffer(false);
  1332. }
  1333. }
  1334. size32_t beginNested()
  1335. {
  1336. if (nested++==0)
  1337. if (bufpos==ROW_WRITER_BUFFERSIZE)
  1338. flushBuffer(false);
  1339. size32_t ret = bufpos+extbuf.length();
  1340. size32_t sz = 0;
  1341. put(sizeof(sz),&sz);
  1342. return ret;
  1343. }
  1344. void endNested(size32_t pos)
  1345. {
  1346. size32_t sz = bufpos+extbuf.length()-(pos + sizeof(size32_t));
  1347. size32_t wr = sizeof(size32_t);
  1348. byte *out = (byte *)&sz;
  1349. if (pos<ROW_WRITER_BUFFERSIZE) {
  1350. size32_t space = ROW_WRITER_BUFFERSIZE-pos;
  1351. if (space>wr)
  1352. space = wr;
  1353. memcpy(buf+pos,out,space);
  1354. wr -= space;
  1355. if (wr==0) {
  1356. --nested;
  1357. return; // quick exit
  1358. }
  1359. out += space;
  1360. pos += space;
  1361. }
  1362. extbuf.writeDirect(pos-ROW_WRITER_BUFFERSIZE,wr,out);
  1363. --nested;
  1364. }
  1365. };
  1366. #ifdef TRACE_CREATE
  1367. unsigned CRowStreamWriter::wrnum=0;
  1368. #endif
  1369. IExtRowWriter *createRowWriter(IFile *file,IOutputRowSerializer *serializer,IEngineRowAllocator *allocator,bool grouped, bool tallycrc, bool extend)
  1370. {
  1371. Owned<IFileIO> fileio = file->open(extend?IFOwrite:IFOcreate);
  1372. if (!fileio)
  1373. return NULL;
  1374. Owned<IFileIOStream> stream = createIOStream(fileio);
  1375. if (extend)
  1376. stream->seek(0,IFSend);
  1377. return createRowWriter(stream,serializer,allocator,grouped,tallycrc,true);
  1378. }
  1379. IExtRowWriter *createRowWriter(IFileIOStream *strm,IOutputRowSerializer *serializer,IEngineRowAllocator *allocator,bool grouped, bool tallycrc, bool autoflush)
  1380. {
  1381. Owned<CRowStreamWriter> writer = new CRowStreamWriter(strm, serializer, allocator, grouped, tallycrc, autoflush);
  1382. return writer.getClear();
  1383. }
  1384. class CDiskMerger : public CInterface, implements IDiskMerger
  1385. {
  1386. IArrayOf<IFile> tempfiles;
  1387. IRowStream **strms;
  1388. Linked<IRecordSize> irecsize;
  1389. StringAttr tempnamebase;
  1390. Linked<IRowLinkCounter> linker;
  1391. Linked<IRowInterfaces> rowInterfaces;
  1392. public:
  1393. IMPLEMENT_IINTERFACE;
  1394. CDiskMerger(IRowInterfaces *_rowInterfaces, IRowLinkCounter *_linker, const char *_tempnamebase)
  1395. : rowInterfaces(_rowInterfaces), linker(_linker), tempnamebase(_tempnamebase)
  1396. {
  1397. strms = NULL;
  1398. }
  1399. ~CDiskMerger()
  1400. {
  1401. for (unsigned i=0;i<tempfiles.ordinality();i++) {
  1402. if (strms&&strms[i])
  1403. strms[i]->Release();
  1404. tempfiles.item(i).remove();
  1405. }
  1406. free(strms);
  1407. }
  1408. IRowWriter *createWriteBlock()
  1409. {
  1410. StringBuffer tempname(tempnamebase);
  1411. tempname.append('.').append(tempfiles.ordinality()).append('_').append((__int64)GetCurrentThreadId()).append('_').append((unsigned)GetCurrentProcessId());
  1412. IFile *file = createIFile(tempname.str());
  1413. tempfiles.append(*file);
  1414. return createRowWriter(file,rowInterfaces->queryRowSerializer(),rowInterfaces->queryRowAllocator(),false,false,false); // flushed by close
  1415. }
  1416. void put(const void **rows,unsigned numrows)
  1417. {
  1418. Owned<IRowWriter> out = createWriteBlock();
  1419. for (unsigned i=0;i<numrows;i++)
  1420. out->putRow(rows[i]);
  1421. }
  1422. void putIndirect(const void ***rowptrs,unsigned numrows)
  1423. {
  1424. Owned<IRowWriter> out = createWriteBlock();
  1425. for (unsigned i=0;i<numrows;i++)
  1426. out->putRow(*(rowptrs[i]));
  1427. }
  1428. virtual void put(ISortedRowProvider *rows)
  1429. {
  1430. Owned<IRowWriter> out = createWriteBlock();
  1431. void * row;
  1432. while(row = rows->getNextSorted())
  1433. out->putRow(row);
  1434. }
  1435. IRowStream *merge(ICompare *icompare, bool partdedup)
  1436. {
  1437. unsigned numstrms = tempfiles.ordinality();
  1438. strms = (IRowStream **)calloc(numstrms,sizeof(IRowStream *));
  1439. unsigned i;
  1440. for (i=0;i<numstrms;i++) {
  1441. strms[i] = createSimpleRowStream(&tempfiles.item(i), rowInterfaces);
  1442. }
  1443. if (numstrms==1)
  1444. return LINK(strms[0]);
  1445. if (icompare)
  1446. return createRowStreamMerger(numstrms, strms, icompare, partdedup, linker);
  1447. return createConcatRowStream(numstrms,strms);
  1448. }
  1449. virtual count_t mergeTo(IRowWriter *dest, ICompare *icompare, bool partdedup)
  1450. {
  1451. count_t count = 0;
  1452. Owned<IRowStream> mergedStream = merge(icompare, partdedup);
  1453. loop
  1454. {
  1455. const void *row = mergedStream->nextRow();
  1456. if (!row)
  1457. return count;
  1458. dest->putRow(row); // takes ownership
  1459. ++count;
  1460. }
  1461. return count;
  1462. }
  1463. };
  1464. IDiskMerger *createDiskMerger(IRowInterfaces *rowInterfaces, IRowLinkCounter *linker, const char *tempnamebase)
  1465. {
  1466. return new CDiskMerger(rowInterfaces, linker, tempnamebase);
  1467. }