thkeydiffslave.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jiface.hpp"
  14. #include "jtime.hpp"
  15. #include "jfile.ipp"
  16. #include "keydiff.hpp"
  17. #include "backup.hpp"
  18. #include "thexception.hpp"
  19. #include "thmfilemanager.hpp"
  20. #include "thbuf.hpp"
  21. #include "slave.ipp"
  22. #include "thactivityutil.ipp"
  23. class CKeyDiffSlave : public ProcessSlaveActivity
  24. {
  25. IHThorKeyDiffArg *helper;
  26. Owned<IPartDescriptor> originalIndexPart, updatedIndexPart, patchPart;
  27. Owned<IPartDescriptor> originalIndexTlkPart, updatedIndexTlkPart, patchTlkPart;
  28. Owned<IKeyDiffGenerator> diffGenerator, tlkDiffGenerator;
  29. bool tlk, copyTlk;
  30. unsigned patchCrc, tlkPatchCrc;
  31. public:
  32. CKeyDiffSlave(CGraphElementBase *container) : ProcessSlaveActivity(container)
  33. {
  34. helper = NULL;
  35. tlk = false;
  36. copyTlk = globals->getPropBool("@diffCopyTlk", true); // because tlk can have meta data and diff/patch does not support
  37. }
  38. ~CKeyDiffSlave()
  39. {
  40. }
  41. void init(MemoryBuffer &data, MemoryBuffer &slaveData)
  42. {
  43. helper = (IHThorKeyDiffArg *)queryHelper();
  44. bool active;
  45. data.read(active);
  46. if (!active)
  47. {
  48. abortSoon = true;
  49. return;
  50. }
  51. originalIndexPart.setown(deserializePartFileDescriptor(data));
  52. updatedIndexPart.setown(deserializePartFileDescriptor(data));
  53. patchPart.setown(deserializePartFileDescriptor(data));
  54. if (firstNode())
  55. {
  56. data.read(tlk);
  57. if (tlk)
  58. {
  59. originalIndexTlkPart.setown(deserializePartFileDescriptor(data));
  60. updatedIndexTlkPart.setown(deserializePartFileDescriptor(data));
  61. patchTlkPart.setown(deserializePartFileDescriptor(data));
  62. }
  63. }
  64. }
  65. virtual void process()
  66. {
  67. processed = THORDATALINK_STARTED;
  68. if (abortSoon) return;
  69. StringBuffer originalFilePart, updatedFilePart;
  70. OwnedRoxieString origName(helper->getOriginalName());
  71. OwnedRoxieString updatedName(helper->getUpdatedName());
  72. locateFilePartPath(this, origName, *originalIndexPart, originalFilePart);
  73. locateFilePartPath(this, updatedName, *updatedIndexPart, updatedFilePart);
  74. StringBuffer patchFilePath;
  75. getPartFilename(*patchPart, 0, patchFilePath);
  76. if (globals->getPropBool("@replicateAsync", true))
  77. cancelReplicates(this, *patchPart);
  78. ensureDirectoryForFile(patchFilePath.str());
  79. diffGenerator.setown(createKeyDiffGenerator(originalFilePart.str(), updatedFilePart.str(), patchFilePath.str(), 0, true, COMPRESS_METHOD_LZMA));
  80. ActPrintLog("KEYPATCH: handling original index = %s, updated index = %s, output patch = %s", originalFilePart.str(), updatedFilePart.str(), patchFilePath.str());
  81. if (tlk)
  82. {
  83. if (globals->getPropBool("@replicateAsync", true))
  84. cancelReplicates(this, *patchTlkPart);
  85. if (!copyTlk)
  86. {
  87. StringBuffer tmp;
  88. locateFilePartPath(this, tmp.clear().append(origName).append(" [TLK]").str(), *originalIndexTlkPart, originalFilePart.clear());
  89. locateFilePartPath(this, tmp.clear().append(updatedName).append(" [TLK]").str(), *updatedIndexTlkPart, updatedFilePart.clear());
  90. getPartFilename(*patchTlkPart, 0, tmp.clear());
  91. tlkDiffGenerator.setown(createKeyDiffGenerator(originalFilePart.str(), updatedFilePart.str(), tmp.str(), 0, true, COMPRESS_METHOD_LZMA));
  92. }
  93. }
  94. try
  95. {
  96. diffGenerator->run();
  97. diffGenerator->logStats(); // JCSMORE - may want some into svg graph.
  98. patchCrc = diffGenerator->queryPatchFileCRC();
  99. diffGenerator.clear();
  100. if (tlk && !copyTlk)
  101. {
  102. tlkDiffGenerator->run();
  103. tlkDiffGenerator->logStats();
  104. tlkPatchCrc = tlkDiffGenerator->queryPatchFileCRC();
  105. tlkDiffGenerator.clear();
  106. }
  107. try
  108. {
  109. if (patchPart->numCopies() > 1)
  110. doReplicate(this, *patchPart);
  111. if (tlk && copyTlk)
  112. {
  113. StringBuffer patchFilePathTlk, updatedFilePartTlk, tmp;
  114. getPartFilename(*patchTlkPart, 0, patchFilePathTlk);
  115. OwnedRoxieString updatedName(helper->getUpdatedName());
  116. locateFilePartPath(this, tmp.append(updatedName).append(" [TLK]").str(), *updatedIndexTlkPart, updatedFilePartTlk);
  117. OwnedIFile dstIFileTlk = createIFile(patchFilePathTlk.str());
  118. OwnedIFile updatedIFileTlk = createIFile(updatedFilePartTlk.str());
  119. copyFile(dstIFileTlk, updatedIFileTlk);
  120. if (patchTlkPart->numCopies() > 1)
  121. doReplicate(this, *patchTlkPart);
  122. }
  123. }
  124. catch (IException *e)
  125. {
  126. ActPrintLog(e, "Failure to create backup patch files");
  127. throw;
  128. }
  129. }
  130. catch (IException *e)
  131. {
  132. ActPrintLog(e, "KEYPATH exception");
  133. throw;
  134. }
  135. }
  136. virtual void endProcess()
  137. {
  138. if (processed & THORDATALINK_STARTED)
  139. processed |= THORDATALINK_STOPPED;
  140. }
  141. virtual void processDone(MemoryBuffer &mb)
  142. {
  143. if (abortSoon)
  144. return;
  145. StringBuffer tmpStr;
  146. Owned<IFile> ifile = createIFile(getPartFilename(*patchPart, 0, tmpStr).str());
  147. offset_t sz = ifile->size();
  148. if (-1 != sz)
  149. container.queryJob().queryIDiskUsage().increase(sz);
  150. mb.append(sz);
  151. CDateTime createTime, modifiedTime, accessedTime;
  152. ifile->getTime(&createTime, &modifiedTime, &accessedTime);
  153. // round file time down to nearest sec. Nanosec accuracy is not preserved elsewhere and can lead to mismatch later.
  154. unsigned hour, min, sec, nanosec;
  155. modifiedTime.getTime(hour, min, sec, nanosec);
  156. modifiedTime.setTime(hour, min, sec, 0);
  157. modifiedTime.serialize(mb);
  158. mb.append(patchCrc);
  159. if (tlk)
  160. {
  161. Owned<IFile> ifile = createIFile(getPartFilename(*patchTlkPart, 0, tmpStr.clear()).str());
  162. offset_t sz = ifile->size();
  163. if (-1 != sz)
  164. container.queryJob().queryIDiskUsage().increase(sz);
  165. mb.append(sz);
  166. CDateTime createTime, modifiedTime, accessedTime;
  167. ifile->getTime(&createTime, &modifiedTime, &accessedTime);
  168. // round file time down to nearest sec. Nanosec accuracy is not preserved elsewhere and can lead to mismatch later.
  169. unsigned hour, min, sec, nanosec;
  170. modifiedTime.getTime(hour, min, sec, nanosec);
  171. modifiedTime.setTime(hour, min, sec, 0);
  172. modifiedTime.serialize(mb);
  173. if (!copyTlk)
  174. mb.append(tlkPatchCrc);
  175. }
  176. }
  177. };
  178. activityslaves_decl CActivityBase *createKeyDiffSlave(CGraphElementBase *container)
  179. {
  180. return new CKeyDiffSlave(container);
  181. }