| 430 | |
| 431 | |
| 432 | Value dumpdb(const Array& params, bool fHelp) { |
| 433 | if (fHelp || params.size() > 2) |
| 434 | throw runtime_error( |
| 435 | "dumpdb \"[key_prefix_type]\" \"[file_path]\"\n" |
| 436 | "\ndump db data to file\n" |
| 437 | "\nArguments:\n" |
| 438 | "1. \"key_prefix_type\" (string, optional) the data key prefix type, * is all data, default is *\n" |
| 439 | "2. \"file_path\" (string, optional) the output file path, if empty output to stdout, default is empty.\n" |
| 440 | "\nResult:\n" |
| 441 | "\nExamples:\n" |
| 442 | + HelpExampleCli("dumpdb", "") + "\nAs json rpc\n" + HelpExampleRpc("dumpdb", "") |
| 443 | ); |
| 444 | |
| 445 | string prefixTypeStr = ""; |
| 446 | if (params.size() > 0) |
| 447 | prefixTypeStr = params[0].get_str(); |
| 448 | string filePath = ""; |
| 449 | if (params.size() > 1) |
| 450 | filePath = params[1].get_str(); |
| 451 | |
| 452 | struct FileCloser { |
| 453 | FILE *file = nullptr; |
| 454 | ~FileCloser() { |
| 455 | if (file != nullptr) { |
| 456 | fclose(file); |
| 457 | file = nullptr; |
| 458 | } |
| 459 | } |
| 460 | }; |
| 461 | |
| 462 | FILE *file = nullptr; |
| 463 | FileCloser fileCloser; |
| 464 | if (!filePath.empty()) { |
| 465 | file = fopen(filePath.c_str(), "w"); |
| 466 | if (file == nullptr) { |
| 467 | throw JSONRPCError(RPC_INVALID_PARAMS, strprintf("opten file error! file=%s", |
| 468 | filePath)); |
| 469 | } |
| 470 | fileCloser.file = file; |
| 471 | } else { |
| 472 | file = stdout; |
| 473 | } |
| 474 | |
| 475 | if (!prefixTypeStr.empty() && prefixTypeStr != "*") { |
| 476 | dbk::PrefixType prefixType = dbk::ParseKeyPrefixType(prefixTypeStr); |
| 477 | if (prefixType == dbk::EMPTY) |
| 478 | throw JSONRPCError(RPC_INVALID_PARAMS, strprintf("unsupported db data key prefix type=%s", |
| 479 | prefixTypeStr)); |
| 480 | DumpDbOne(file, prefixType, prefixTypeStr); |
| 481 | } else { |
| 482 | DumpDbAll(file); |
| 483 | } |
| 484 | |
| 485 | return Object(); |
| 486 | } |
| 487 | |
| 488 | template<int32_t PREFIX_TYPE, typename KeyType, typename ValueType> |
| 489 | Object UndoLogToJson(CCompositeKVCache<PREFIX_TYPE, KeyType, ValueType> &cache, const CDbOpLog &opLog) { |
nothing calls this directly
no test coverage detected