fvquerysource.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "jliball.hpp"
  15. #include "eclrtl.hpp"
  16. #include "hqlexpr.hpp"
  17. #include "hqlthql.hpp"
  18. #include "fvresultset.ipp"
  19. #include "fileview.hpp"
  20. #include "fvquerysource.ipp"
  21. #include "fvwugen.hpp"
  22. QueryDataSource::QueryDataSource(IConstWUResult * _wuResult, const char * _wuid, const char * _username, const char * _password)
  23. {
  24. wuResult.set(_wuResult);
  25. wuid.set(_wuid);
  26. username.set(_username);
  27. password.set(_password);
  28. }
  29. QueryDataSource::~QueryDataSource()
  30. {
  31. if (browseWuid)
  32. {
  33. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  34. factory->deleteWorkUnit(browseWuid);
  35. }
  36. }
  37. bool QueryDataSource::createBrowseWU()
  38. {
  39. StringAttr dataset, datasetDefs;
  40. StringAttrAdaptor a1(dataset), a2(datasetDefs);
  41. wuResult->getResultDataset(a1, a2);
  42. if (!dataset || !datasetDefs)
  43. return false;
  44. StringBuffer fullText;
  45. fullText.append(datasetDefs).append(dataset);
  46. OwnedHqlExpr parsed = parseQuery(fullText.str());
  47. if (!parsed)
  48. return false;
  49. HqlExprAttr selectFields = parsed.getLink();
  50. if (selectFields->getOperator() == no_output)
  51. selectFields.set(selectFields->queryChild(0));
  52. OwnedHqlExpr browseWUcode = buildQueryViewerEcl(selectFields);
  53. if (!browseWUcode)
  54. return false;
  55. returnedRecord.set(browseWUcode->queryChild(0)->queryRecord());
  56. StringAttr tempAttr;
  57. StringAttrAdaptor temp(tempAttr);
  58. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  59. Owned<IConstWorkUnit> parent = factory->openWorkUnit(wuid, false);
  60. SCMStringBuffer user;
  61. StringAttrAdaptor acluster(cluster);
  62. parent->getClusterName(acluster);
  63. parent->getUser(user);
  64. Owned<IWorkUnit> workunit = factory->createWorkUnit(NULL, "fileViewer", user.str());
  65. workunit->setUser(user.str());
  66. workunit->setClusterName(cluster);
  67. workunit->setCustomerId(parent->getCustomerId(temp).str());
  68. workunit->setCompareMode(CompareModeOff); // ? parent->getCompareMode()
  69. StringAttrAdaptor bwa(browseWuid); workunit->getWuid(bwa);
  70. workunit->setDebugValueInt("importImplicitModules", false, true);
  71. workunit->setDebugValueInt("importAllModules", false, true);
  72. workunit->setDebugValueInt("forceFakeThor", 1, true);
  73. StringBuffer jobName;
  74. jobName.append("FileView for ").append(wuid).append(":").append("x");
  75. workunit->setJobName(jobName.str());
  76. StringBuffer eclText;
  77. toECL(browseWUcode, eclText, true);
  78. Owned<IWUQuery> query = workunit->updateQuery();
  79. query->setQueryText(eclText.str());
  80. query->setQueryName(jobName.str());
  81. return true;
  82. }
  83. bool QueryDataSource::init()
  84. {
  85. return createBrowseWU();
  86. }
  87. void QueryDataSource::improveLocation(__int64 row, RowLocation & location)
  88. {
  89. #if 0
  90. if (!diskMeta->isFixedSize())
  91. return;
  92. if (location.bestRow <= row && location.bestRow + DISKREAD_PAGE_SIZE > row)
  93. return;
  94. assertex(row >= 0);
  95. //Align the row so the chunks don't overlap....
  96. location.bestRow = (row / DISKREAD_PAGE_SIZE) * DISKREAD_PAGE_SIZE;
  97. location.bestOffset = location.bestRow * diskMeta->fixedSize();
  98. #endif
  99. }
  100. bool QueryDataSource::loadBlock(__int64 startRow, offset_t startOffset)
  101. {
  102. MemoryBuffer temp;
  103. //enter scope....>
  104. {
  105. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  106. Owned<IWorkUnit> wu = factory->updateWorkUnit(browseWuid);
  107. Owned<IWUResult> lower = wu->updateVariableByName(LOWER_LIMIT_ID);
  108. lower->setResultInt(startOffset);
  109. lower->setResultStatus(ResultStatusSupplied);
  110. Owned<IWUResult> dataResult = wu->updateResultBySequence(0);
  111. dataResult->setResultRaw(0, NULL, ResultFormatRaw);
  112. dataResult->setResultStatus(ResultStatusUndefined);
  113. wu->clearExceptions();
  114. if (wu->getState() != WUStateUnknown)
  115. wu->setState(WUStateCompiled);
  116. //Owned<IWUResult> count = wu->updateVariableByName(RECORD_LIMIT_ID);
  117. //count->setResultInt64(fetchSize);
  118. }
  119. //Resubmit the query...
  120. submitWorkUnit(browseWuid, username, password);
  121. WUState finalState = waitForWorkUnitToComplete(browseWuid, -1, true);
  122. if(!((finalState == WUStateCompleted) || (finalState == WUStateWait)))
  123. return false;
  124. //Now extract the results...
  125. Owned<IWorkUnitFactory> factory = getWorkUnitFactory();
  126. Owned<IConstWorkUnit> wu = factory->openWorkUnit(browseWuid, false);
  127. Owned<IConstWUResult> dataResult = wu->getResultBySequence(0);
  128. MemoryBuffer2IDataVal xxx(temp); dataResult->getResultRaw(xxx, NULL, NULL);
  129. if (temp.length() == 0)
  130. return false;
  131. RowBlock * rows;
  132. if (returnedMeta->isFixedSize())
  133. rows = new FilePosFixedRowBlock(temp, startRow, startOffset, returnedMeta->fixedSize());
  134. else
  135. rows = new FilePosVariableRowBlock(temp, startRow, startOffset, returnedMeta, true);
  136. cache.addRowsOwn(rows);
  137. return true;
  138. }