fvwusource.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "platform.h"
  15. #include "jliball.hpp"
  16. #include "eclrtl.hpp"
  17. #include "fvresultset.ipp"
  18. #include "fileview.hpp"
  19. #include "fvwusource.ipp"
  20. #include "eclhelper.hpp"
  21. #include "fvdatasource.hpp"
  22. WorkUnitDataSource::WorkUnitDataSource(IConstWUResult * _wuResult, const char * _wuid)
  23. {
  24. wuResult.set(_wuResult);
  25. wuid.set(_wuid);
  26. totalRows = wuResult->getResultTotalRowCount();
  27. if (totalRows == -1)
  28. totalRows = 0;
  29. totalSize = wuResult->getResultRawSize(NULL, NULL);
  30. }
  31. bool WorkUnitDataSource::init()
  32. {
  33. return setReturnedInfoFromResult();
  34. }
  35. __int64 WorkUnitDataSource::numRows(bool force)
  36. {
  37. return totalRows;
  38. }
  39. //---------------------------------------------------------------------------
  40. FullWorkUnitDataSource::FullWorkUnitDataSource(IConstWUResult * _wuResult, const char * _wuid) : WorkUnitDataSource(_wuResult, _wuid)
  41. {
  42. }
  43. bool FullWorkUnitDataSource::init()
  44. {
  45. bool ok = WorkUnitDataSource::init();
  46. if (ok)
  47. {
  48. MemoryBuffer temp;
  49. MemoryBuffer2IDataVal xxx(temp);
  50. //Nasty. Single sets are represented as the same way as datasets (with an extra flag for all)
  51. //however need to represent as a single row containing a set, which has a different format.
  52. if (wuResult->isResultScalar() && returnedMeta->isSingleSet())
  53. {
  54. temp.append(wuResult->getResultIsAll());
  55. temp.append((size32_t)wuResult->getResultRawSize(0, 0));
  56. }
  57. wuResult->getResultRaw(xxx, NULL, NULL);
  58. if (returnedMeta->isFixedSize())
  59. rows.setown(new FixedRowBlock(temp, 0, 0, returnedMeta->fixedSize()));
  60. else
  61. rows.setown(new VariableRowBlock(temp, 0, 0, returnedRecordSize, true));
  62. }
  63. return ok;
  64. }
  65. bool FullWorkUnitDataSource::fetchRowData(MemoryBuffer & out, __int64 offset)
  66. {
  67. size32_t length;
  68. const void * data = rows->fetchRow(offset, length);
  69. if (!data)
  70. return false;
  71. out.append(length, data);
  72. return true;
  73. }
  74. bool FullWorkUnitDataSource::getRowData(__int64 row, size32_t & length, const void * & data, unsigned __int64 & offset)
  75. {
  76. data = rows->getRow(row, length, offset);
  77. return (data != NULL);
  78. }
  79. //---------------------------------------------------------------------------
  80. PagedWorkUnitDataSource::PagedWorkUnitDataSource(IConstWUResult * _wuResult, const char * _wuid) : WorkUnitDataSource(_wuResult, _wuid)
  81. {
  82. }
  83. bool PagedWorkUnitDataSource::init()
  84. {
  85. return WorkUnitDataSource::init();
  86. }
  87. bool PagedWorkUnitDataSource::getRowData(__int64 row, size32_t & length, const void * & data, unsigned __int64 & offset)
  88. {
  89. if ((row < 0) || ((unsigned __int64)row > totalRows))
  90. return false;
  91. RowLocation location;
  92. loop
  93. {
  94. if (cache.getCacheRow(row, location))
  95. {
  96. length = location.matchLength;
  97. data = location.matchRow;
  98. return true;
  99. }
  100. if (!loadBlock(location.bestRow, location.bestOffset))
  101. return false;
  102. }
  103. }
  104. bool PagedWorkUnitDataSource::fetchRowData(MemoryBuffer & out, __int64 offset)
  105. {
  106. MemoryBuffer temp;
  107. MemoryBuffer2IDataVal wrapper(out);
  108. wuResult->getResultRaw(wrapper, offset, returnedMeta->getMaxRecordSize(), NULL, NULL);
  109. if (temp.length() == 0)
  110. return false;
  111. return true;
  112. }
  113. bool PagedWorkUnitDataSource::loadBlock(__int64 startRow, offset_t startOffset)
  114. {
  115. MemoryBuffer temp;
  116. MemoryBuffer2IDataVal xxx(temp);
  117. RowBlock * rows;
  118. if (returnedMeta->isFixedSize())
  119. {
  120. unsigned fixedSize = returnedMeta->fixedSize();
  121. unsigned readSize = (WU_BLOCK_SIZE / fixedSize) * fixedSize;
  122. wuResult->getResultRaw(xxx, startOffset, readSize, NULL, NULL);
  123. if (temp.length() == 0)
  124. return false;
  125. rows = new FixedRowBlock(temp, startRow, startOffset, fixedSize);
  126. }
  127. else
  128. {
  129. wuResult->getResultRaw(xxx, startOffset, WU_BLOCK_SIZE, NULL, NULL);
  130. if (temp.length() == 0)
  131. return false;
  132. rows = new VariableRowBlock(temp, startRow, startOffset, returnedRecordSize, startOffset + temp.length() == totalSize);
  133. }
  134. cache.addRowsOwn(rows);
  135. return true;
  136. }