1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 #pragma once
6 #ifndef ROCKSDB_LITE
7 
8 #include "rocksdb/sst_dump_tool.h"
9 
10 #include <memory>
11 #include <string>
12 #include "db/dbformat.h"
13 #include "file/writable_file_writer.h"
14 #include "options/cf_options.h"
15 
16 namespace ROCKSDB_NAMESPACE {
17 
18 class SstFileDumper {
19  public:
20   explicit SstFileDumper(const Options& options, const std::string& file_name,
21                          bool verify_checksum, bool output_hex,
22                          bool decode_blob_index);
23 
24   Status ReadSequential(bool print_kv, uint64_t read_num, bool has_from,
25                         const std::string& from_key, bool has_to,
26                         const std::string& to_key,
27                         bool use_from_as_prefix = false);
28 
29   Status ReadTableProperties(
30       std::shared_ptr<const TableProperties>* table_properties);
GetReadNumber()31   uint64_t GetReadNumber() { return read_num_; }
GetInitTableProperties()32   TableProperties* GetInitTableProperties() { return table_properties_.get(); }
33 
34   Status VerifyChecksum();
35   Status DumpTable(const std::string& out_filename);
getStatus()36   Status getStatus() { return init_result_; }
37 
38   int ShowAllCompressionSizes(
39       size_t block_size,
40       const std::vector<std::pair<CompressionType, const char*>>&
41           compression_types);
42 
43  private:
44   // Get the TableReader implementation for the sst file
45   Status GetTableReader(const std::string& file_path);
46   Status ReadTableProperties(uint64_t table_magic_number,
47                              RandomAccessFileReader* file, uint64_t file_size);
48 
49   uint64_t CalculateCompressedTableSize(const TableBuilderOptions& tb_options,
50                                         size_t block_size,
51                                         uint64_t* num_data_blocks);
52 
53   Status SetTableOptionsByMagicNumber(uint64_t table_magic_number);
54   Status SetOldTableOptions();
55 
56   // Helper function to call the factory with settings specific to the
57   // factory implementation
58   Status NewTableReader(const ImmutableCFOptions& ioptions,
59                         const EnvOptions& soptions,
60                         const InternalKeyComparator& internal_comparator,
61                         uint64_t file_size,
62                         std::unique_ptr<TableReader>* table_reader);
63 
64   std::string file_name_;
65   uint64_t read_num_;
66   bool verify_checksum_;
67   bool output_hex_;
68   bool decode_blob_index_;
69   EnvOptions soptions_;
70 
71   // options_ and internal_comparator_ will also be used in
72   // ReadSequential internally (specifically, seek-related operations)
73   Options options_;
74 
75   Status init_result_;
76   std::unique_ptr<TableReader> table_reader_;
77   std::unique_ptr<RandomAccessFileReader> file_;
78 
79   const ImmutableCFOptions ioptions_;
80   const MutableCFOptions moptions_;
81   InternalKeyComparator internal_comparator_;
82   std::unique_ptr<TableProperties> table_properties_;
83 };
84 
85 }  // namespace ROCKSDB_NAMESPACE
86 
87 #endif  // ROCKSDB_LITE
88