dumpkey.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "jhtree.hpp"
  16. #include "ctfile.hpp"
  17. void fatal(const char *format, ...) __attribute__((format(printf, 1, 2)))
  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. openLogFile(logname.append("log").str());
  80. Owned <IKeyIndex> index;
  81. const char * keyName = globals->queryProp("keyfile");
  82. if (keyName)
  83. index.setown(createKeyIndex(keyName, 0, false, false));
  84. else
  85. usage();
  86. Owned <IKeyCursor> cursor = index->getCursor(NULL);
  87. size32_t key_size = index->keySize();
  88. Owned<IFile> in = createIFile(keyName);
  89. Owned<IFileIO> io = in->open(IFOread);
  90. if (!io)
  91. throw MakeStringException(999, "Failed to open file %s", keyName);
  92. Owned<CKeyHdr> header = new CKeyHdr;
  93. MemoryAttr block(sizeof(KeyHdr));
  94. io->read(0, sizeof(KeyHdr), (void *)block.get());
  95. header->load(*(KeyHdr*)block.get());
  96. unsigned nodeSize = header->getNodeSize();
  97. if (!optRaw)
  98. {
  99. printf("Key '%s'\nkeySize=%d NumParts=%x, Top=%d\n", keyName, key_size, index->numParts(), index->isTopLevelKey());
  100. printf("File size = %"I64F"d, nodes = %"I64F"d\n", in->size(), in->size() / nodeSize - 1);
  101. printf("rootoffset=%"I64F"d[%"I64F"d]\n", header->getRootFPos(), header->getRootFPos()/nodeSize);
  102. }
  103. char *buffer = (char*)alloca(key_size);
  104. if (globals->hasProp("node"))
  105. {
  106. if (stricmp(globals->queryProp("node"), "all")==0)
  107. {
  108. }
  109. else
  110. {
  111. int node = globals->getPropInt("node");
  112. if (node != 0)
  113. index->dumpNode(stdout, node * nodeSize, globals->getPropInt("recs", 0), optRaw);
  114. }
  115. }
  116. else if (globals->hasProp("fpos"))
  117. {
  118. index->dumpNode(stdout, globals->getPropInt("fpos"), globals->getPropInt("recs", 0), optRaw);
  119. }
  120. else
  121. {
  122. bool backwards=false;
  123. bool ok;
  124. if (globals->hasProp("end"))
  125. {
  126. memset(buffer, 0, key_size);
  127. strcpy(buffer, globals->queryProp("end"));
  128. ok = cursor->ltEqual(buffer, buffer);
  129. backwards = true;
  130. }
  131. else if (globals->hasProp("start"))
  132. {
  133. memset(buffer, 0, key_size);
  134. strcpy(buffer, globals->queryProp("start"));
  135. ok = cursor->gtEqual(buffer, buffer);
  136. }
  137. else
  138. ok = cursor->first(buffer);
  139. unsigned count = globals->getPropInt("recs", 1);
  140. while (ok && count--)
  141. {
  142. offset_t pos = cursor->getFPos();
  143. unsigned __int64 seq = cursor->getSequence();
  144. size32_t size = cursor->getSize();
  145. if (optRaw)
  146. {
  147. fwrite(buffer, 1, size, stdout);
  148. }
  149. else if (optHex)
  150. {
  151. for (unsigned i = 0; i < size; i++)
  152. printf("%02x", ((unsigned char) buffer[i]) & 0xff);
  153. printf(" :%"I64F"u:%012"I64F"x\n", seq, pos);
  154. }
  155. else
  156. printf("%.*s :%"I64F"u:%012"I64F"x\n", size, buffer, seq, pos);
  157. if (backwards)
  158. ok = cursor->prev(buffer);
  159. else
  160. ok = cursor->next(buffer);
  161. }
  162. }
  163. }
  164. catch (IException *E)
  165. {
  166. StringBuffer msg;
  167. E->errorMessage(msg);
  168. E->Release();
  169. fatal("%s", msg.str());
  170. }
  171. releaseAtoms();
  172. ExitModuleObjects();
  173. return 0;
  174. }