dumpkey.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "jliball.hpp"
  14. #include "jhtree.hpp"
  15. #include "ctfile.hpp"
  16. void fatal(const char *format, ...) __attribute__((format(printf, 1, 2)));
  17. void fatal(const char *format, ...)
  18. {
  19. va_list args;
  20. va_start(args, format);
  21. vfprintf(stderr, format, args);
  22. va_end(args);
  23. fflush(stderr);
  24. releaseAtoms();
  25. ExitModuleObjects();
  26. _exit(2);
  27. }
  28. bool optHex = false;
  29. bool optRaw = false;
  30. void usage()
  31. {
  32. fprintf(stderr, "Usage: dumpkey dataset [options]\n"
  33. "Options:\n"
  34. " node=[n] - dump node n (0 = just header)\n"
  35. " fpos=[n] - dump node at offset fpos\n"
  36. " recs=[n] - dump n rows\n"
  37. " -H - hex display\n"
  38. " -R - raw output\n"
  39. );
  40. fflush(stderr);
  41. releaseAtoms();
  42. ExitModuleObjects();
  43. _exit(2);
  44. }
  45. void doOption(const char *opt)
  46. {
  47. switch (toupper(opt[1]))
  48. {
  49. case 'H':
  50. optHex = true;
  51. break;
  52. case 'R':
  53. optRaw = true;
  54. break;
  55. default:
  56. usage();
  57. }
  58. }
  59. int main(int argc, const char **argv)
  60. {
  61. #ifdef _WIN32
  62. _setmode( _fileno( stdout ), _O_BINARY );
  63. _setmode( _fileno( stdin ), _O_BINARY );
  64. #endif
  65. Owned<IProperties> globals = createProperties("dumpkey.ini", true);
  66. for (int i = 1; i < argc; i++)
  67. {
  68. if (argv[i][0] == '-')
  69. doOption(argv[i]);
  70. else if (strchr(argv[i], '='))
  71. globals->loadProp(argv[i]);
  72. else
  73. globals->setProp("keyfile", argv[i]);
  74. }
  75. try
  76. {
  77. StringBuffer logname("dumpkey.");
  78. logname.append(GetCachedHostName()).append(".");
  79. StringBuffer lf;
  80. openLogFile(lf, logname.append("log").str());
  81. Owned <IKeyIndex> index;
  82. const char * keyName = globals->queryProp("keyfile");
  83. if (keyName)
  84. index.setown(createKeyIndex(keyName, 0, false, false));
  85. else
  86. usage();
  87. Owned <IKeyCursor> cursor = index->getCursor(NULL);
  88. size32_t key_size = index->keySize();
  89. Owned<IFile> in = createIFile(keyName);
  90. Owned<IFileIO> io = in->open(IFOread);
  91. if (!io)
  92. throw MakeStringException(999, "Failed to open file %s", keyName);
  93. Owned<CKeyHdr> header = new CKeyHdr;
  94. MemoryAttr block(sizeof(KeyHdr));
  95. io->read(0, sizeof(KeyHdr), (void *)block.get());
  96. header->load(*(KeyHdr*)block.get());
  97. unsigned nodeSize = header->getNodeSize();
  98. if (!optRaw)
  99. {
  100. printf("Key '%s'\nkeySize=%d NumParts=%x, Top=%d\n", keyName, key_size, index->numParts(), index->isTopLevelKey());
  101. printf("File size = %"I64F"d, nodes = %"I64F"d\n", in->size(), in->size() / nodeSize - 1);
  102. printf("rootoffset=%"I64F"d[%"I64F"d]\n", header->getRootFPos(), header->getRootFPos()/nodeSize);
  103. }
  104. char *buffer = (char*)alloca(key_size);
  105. if (globals->hasProp("node"))
  106. {
  107. if (stricmp(globals->queryProp("node"), "all")==0)
  108. {
  109. }
  110. else
  111. {
  112. int node = globals->getPropInt("node");
  113. if (node != 0)
  114. index->dumpNode(stdout, node * nodeSize, globals->getPropInt("recs", 0), optRaw);
  115. }
  116. }
  117. else if (globals->hasProp("fpos"))
  118. {
  119. index->dumpNode(stdout, globals->getPropInt("fpos"), globals->getPropInt("recs", 0), optRaw);
  120. }
  121. else
  122. {
  123. bool backwards=false;
  124. bool ok;
  125. if (globals->hasProp("end"))
  126. {
  127. memset(buffer, 0, key_size);
  128. strcpy(buffer, globals->queryProp("end"));
  129. ok = cursor->ltEqual(buffer, buffer);
  130. backwards = true;
  131. }
  132. else if (globals->hasProp("start"))
  133. {
  134. memset(buffer, 0, key_size);
  135. strcpy(buffer, globals->queryProp("start"));
  136. ok = cursor->gtEqual(buffer, buffer);
  137. }
  138. else
  139. ok = cursor->first(buffer);
  140. unsigned count = globals->getPropInt("recs", 1);
  141. while (ok && count--)
  142. {
  143. offset_t pos = cursor->getFPos();
  144. unsigned __int64 seq = cursor->getSequence();
  145. size32_t size = cursor->getSize();
  146. if (optRaw)
  147. {
  148. fwrite(buffer, 1, size, stdout);
  149. }
  150. else if (optHex)
  151. {
  152. for (unsigned i = 0; i < size; i++)
  153. printf("%02x", ((unsigned char) buffer[i]) & 0xff);
  154. printf(" :%"I64F"u:%012"I64F"x\n", seq, pos);
  155. }
  156. else
  157. printf("%.*s :%"I64F"u:%012"I64F"x\n", size, buffer, seq, pos);
  158. if (backwards)
  159. ok = cursor->prev(buffer);
  160. else
  161. ok = cursor->next(buffer);
  162. }
  163. }
  164. }
  165. catch (IException *E)
  166. {
  167. StringBuffer msg;
  168. E->errorMessage(msg);
  169. E->Release();
  170. fatal("%s", msg.str());
  171. }
  172. releaseAtoms();
  173. ExitModuleObjects();
  174. return 0;
  175. }